Get Cell value on click of linkbutton in Datagrid
int count1;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection Conn;
Conn = new SqlConnection("Data Source=AB2\\SQLEXPRESS;Initial Catalog=Tiger;Integrated Security=True");
Conn.Open();
SqlCommand salCmd = new SqlCommand("select count(V.Vendor_name),V.Vendor_code,V.vendor_name,V.contact_name1,V.contact_mobile1 from vendor V INNER join stock S on S.vendor_code = V.Vendor_code group by V.vendor_name,V.Vendor_code,V.contact_name1,V.contact_mobile1", Conn);
SqlDataReader dr = salCmd.ExecuteReader();
while (dr.Read())
{
count1 = (int)dr[0];
string code = dr[1].ToString();
string name = dr[2].ToString();
string name1 = dr[3].ToString();
string mobile1 = dr[4].ToString();
SqlConnection conDataGrid = new SqlConnection("Data Source=AB2\\SQLEXPRESS;Initial Catalog=Tiger;Integrated Security=True");
SqlDataAdapter adapterDataGrid = new SqlDataAdapter("select prod_id,prod_name,quantity,inprice from stock where vendor_code ='" + code + "'", conDataGrid);
DataSet ds = new DataSet();
adapterDataGrid.Fill(ds);
DataGrid dg = new DataGrid();
dg.Width = 600;
dg.GridLines = GridLines.Both;
dg.CellPadding = 1;
dg.ForeColor = System.Drawing.Color.Black;
dg.BackColor = System.Drawing.Color.Beige;
dg.AlternatingItemStyle.BackColor = System.Drawing.Color.Gainsboro;
dg.HeaderStyle.BackColor = System.Drawing.Color.Brown;
dg.DataSource = ds;
dg.AutoGenerateColumns = true;
BoundColumn datagridcol = new BoundColumn();
datagridcol.HeaderText = "ID";
datagridcol.Visible = true ;
datagridcol.DataField = "prod_id";
dg.Columns.Add(datagridcol);
ButtonColumn selectcol = new ButtonColumn();
selectcol.ButtonType = ButtonColumnType.LinkButton;
selectcol.Text = "ChangeVendor";
selectcol.CommandName = "Select";
dg.Columns.Add(selectcol);
dg.DataBind();
Label lb = new Label();
lb.Visible = true;
lb.Font.Bold = true;
string valls = name + " " + " " + name1 + " " + " " + mobile1;
PlaceHolder1.Controls.Add (dg);
}
Conn.Close();
}
protected void dg_ItemCommand(object source, DataGridCommandEventArgs e)
{
string productid;
TableCell pid = e.Item.Cells[0];
productid = pid.Text;
Response.Write(productid);
}
Above is the code I have used for the dynamic bound column. Now I have created Bound columns for the Link button and prodid.
But when I click the ChangeVendor (Link Button) I have to get the prodid of that cell .
I tried with the Item command but I not able to fetch any value.
Please tell me how I have to fetch the value.
The same way I use to do for the control Datagrid , it worked fine .
Tell me immediately please