I have a aspx page which has gridview with textbox, dropdownlist and i wann some rows comming from database and some to be filled by use it also has a add button to add new row in gridview.
Plzzzzz it urgent reply soonn...
Thank u..
Code.......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class HMS_Pharmacy_Drug_Inventory : System.Web.UI.Page
{
static string strcon = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd, cmd1, cmd2;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// dropdownlist();
SetInitialRow();
//lbl_totalrecord.Text = GridView1.Rows.Count.ToString();
}
}
/*Drug Grid View Here Start*/
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//LinkButton b1 = (LinkButton)GridView1.Rows[rowIndex].Cells[1].FindControl("LinkButton1");
Label l1 = (Label)GridView1.Rows[rowIndex].Cells[1].FindControl("Label1");
TextBox t1 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txt_medi");
TextBox t2 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("txt_batch");
TextBox t3 = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("txt_edt");
TextBox t4 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("txt_qty");
TextBox t5 = (TextBox)GridView1.Rows[rowIndex].Cells[6].FindControl("txt_fqty");
TextBox t6 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("txt_rprice");
TextBox t7 = (TextBox)GridView1.Rows[rowIndex].Cells[8].FindControl("txt_ptr");
TextBox t8 = (TextBox)GridView1.Rows[rowIndex].Cells[9].FindControl("txt_dis");
TextBox t9 = (TextBox)GridView1.Rows[rowIndex].Cells[10].FindControl("txt_rate");
TextBox t10 = (TextBox)GridView1.Rows[rowIndex].Cells[11].FindControl("txt_vat");
TextBox t11 = (TextBox)GridView1.Rows[rowIndex].Cells[12].FindControl("txt_vatr");
TextBox t12 = (TextBox)GridView1.Rows[rowIndex].Cells[13].FindControl("txt_amt");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["Label1"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = t1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = t2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = t3.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = t4.Text;
dtCurrentTable.Rows[i - 1]["Column5"] = t5.Text;
dtCurrentTable.Rows[i - 1]["Column6"] = t6.Text;
dtCurrentTable.Rows[i - 1]["Column7"] = t7.Text;
dtCurrentTable.Rows[i - 1]["Column8"] = t8.Text;
dtCurrentTable.Rows[i - 1]["Column9"] = t9.Text;
dtCurrentTable.Rows[i - 1]["Column10"] =t10.Text;
dtCurrentTable.Rows[i - 1]["Column11"] = t11.Text;
dtCurrentTable.Rows[i - 1]["Column12"] = t12.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Label1", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dt.Columns.Add(new DataColumn("Column5", typeof(string)));
dt.Columns.Add(new DataColumn("Column6", typeof(string)));
dt.Columns.Add(new DataColumn("Column7", typeof(string)));
dt.Columns.Add(new DataColumn("Column8", typeof(string)));
dt.Columns.Add(new DataColumn("Column9", typeof(string)));
dt.Columns.Add(new DataColumn("Column10", typeof(string)));
dt.Columns.Add(new DataColumn("Column11", typeof(string)));
dt.Columns.Add(new DataColumn("Column12", typeof(string)));
dr = dt.NewRow();
dr["Label1"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dr["Column5"] = string.Empty;
dr["Column6"] = string.Empty;
dr["Column7"] = string.Empty;
dr["Column8"] = string.Empty;
dr["Column9"] = string.Empty;
dr["Column10"] = string.Empty;
dr["Column11"] = string.Empty;
dr["Column12"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 1; i < dt.Rows.Count; i++)
{
// Button b1 = (Button)GridView1.Rows[rowIndex].Cells[1].FindControl("Button1");
Label l1 = (Label)GridView1.Rows[rowIndex].Cells[1].FindControl("Label1");
TextBox t1 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txt_medi");
TextBox t2 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("txt_batch");
TextBox t3 = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("txt_edt");
TextBox t4 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("txt_qty");
TextBox t5 = (TextBox)GridView1.Rows[rowIndex].Cells[6].FindControl("txt_fqty");
TextBox t6 = (TextBox)GridView1.Rows[rowIndex].Cells[7].FindControl("txt_rprice");
TextBox t7 = (TextBox)GridView1.Rows[rowIndex].Cells[8].FindControl("txt_ptr");
TextBox t8 = (TextBox)GridView1.Rows[rowIndex].Cells[9].FindControl("txt_dis");
TextBox t9 = (TextBox)GridView1.Rows[rowIndex].Cells[10].FindControl("txt_rate");
TextBox t10 = (TextBox)GridView1.Rows[rowIndex].Cells[11].FindControl("txt_vat");
TextBox t11 = (TextBox)GridView1.Rows[rowIndex].Cells[12].FindControl("txt_vatr");
TextBox t12 = (TextBox)GridView1.Rows[rowIndex].Cells[13].FindControl("txt_amt");
// b1.Text = dt.Rows[i]["Column1"].ToString();
l1.Text = dt.Rows[i]["Column1"].ToString();
t1.Text = dt.Rows[i]["Column2"].ToString();
t2.Text = dt.Rows[i]["Column3"].ToString();
t3.Text = dt.Rows[i]["Column4"].ToString();
t4.Text = dt.Rows[i]["Column5"].ToString();
t5.Text = dt.Rows[i]["Column6"].ToString();
t6.Text = dt.Rows[i]["Column7"].ToString();
t7.Text = dt.Rows[i]["Column8"].ToString();
t8.Text = dt.Rows[i]["Column9"].ToString();
t9.Text = dt.Rows[i]["Column10"].ToString();
t10.Text = dt.Rows[i]["Column11"].ToString();
t11.Text = dt.Rows[i]["Column12"].ToString();
rowIndex++;
}
}
}
}
// [System.Web.Script.Services.ScriptMethod()]
//[System.Web.Services.WebMethod]
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
/*Drug Grid View End*/
protected void LinkButton2_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
//lbl_totalrecord.Text = GridView1.Rows.Count.ToString();
}
}
/* protected void dropdownlist()
{
try
{
//start drug name added droupdown
ddl_search_nm.Items.Add("Select Drug");
con.Open();
cmd = new SqlCommand("select * from Mst_Drug", con);
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
ListItem li = new ListItem();
li.Text = dr["Drug_Name"].ToString();
li.Value = dr["Drug_Id"].ToString();
ddl_search_nm.Items.Add(li);
}
dr.Close();
//End drug name added droupdown
//start Vendor name added droupdown
ddl_vendor.Items.Add("Select Vendor");
cmd1 = new SqlCommand("select * from Mst_Vendor", con);
SqlDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
ListItem li = new ListItem();
li.Text = dr1["Vendor_Firm_Name"].ToString();
li.Value = dr1["Vendor_Id"].ToString();
ddl_vendor.Items.Add(li);
}
dr.Close();
//End Vendor name added droupdown
}
catch (Exception er) { }
finally
{
con.Close();
con.Dispose();
}
}
protected void txt_box_TextChanged(object sender, EventArgs e)
{
try
{
//lbl_msg.Text = txt_box.Text + ":" + txt_strip.Text + ":" + txt_units.Text;
int box=Int32.Parse(txt_box.Text.ToString());
int strip=Int32.Parse(txt_strip.Text.ToString());
int units=Int32.Parse(txt_units.Text.ToString());
txt_total.Text = ((box) + (strip) + (units)).ToString();
}
catch (Exception er)
{
//lbl_msg.Text = er.ToString();
}
}
protected void ddl_search_nm_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
lbl_boxes.Text= "0";
lbl_strip.Text= "0";
lbl_msg.Text = ddl_search_nm.Text.ToString();
con.Open();
cmd2 = new SqlCommand("select * from sec_Drug_Packing where Drug_Id='" + ddl_search_nm.Text.ToString() + "'", con);
SqlDataReader dr = cmd2.ExecuteReader();
if (dr.Read())
{
lbl_boxes.Text = dr.GetValue(2).ToString();
lbl_strip.Text = dr.GetValue(3).ToString();
}
dr.Close();
cmd2.Cancel();
cmd2.Dispose();
}
catch (Exception er) {
lbl_msg.Text = er.ToString();
}
finally
{
con.Close();
con.Dispose();
}
}*/