gridview binding
-
I have add check box in a gridview in eachh row at run time.I have a delete button on page.If it is clicked then all checkbox are checed if true yhen delete corresponding row.I have use Template column for checkbox.and bind an id with it.On running page this id is also visible but i want to do invisible it.also how can i retrieve this id if checkbox is checked. here is code... protected void Page_Load(object sender, EventArgs e) { query="select brok.*,cust.FirstName+' '+cust.LastName as Name from brokermsgrecieved as brok,custdetails as cust where brok.BrokerId='"+Session["broker"]+"' and cust.CustId=brok.FromCustId"; dt = obj.select(query); GridView1.DataSource=dt; GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { CheckBox chk = new CheckBox(); e.Row.Cells[0].Controls.Add(chk); if (e.Row.Cells[1].Text == "True") { e.Row.BackColor = Color.Wheat; } if (e.Row.Cells[1].Text == "False") { e.Row.BackColor = Color.WhiteSmoke; } } protected void Button1_Click(object sender, EventArgs e) { GridView1.DataSource = dt; GridView1.DataBind(); Int16 count =(Int16) GridView1.Rows.Count; for (Int16 i = 1; i < count; i++) { CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].Controls[1]; if (chk.Checked.Equals(true)) { //query="Delete from brokermsgrecieved where " //GridView1.Rows[i].Cells[0].v query = "delete from brokermsgrecieved where MsgId='1'"; obj.insert(query); } } } here is code such that i bind id---- <Columns> asp:TemplateField <ItemTemplate> <%# Eval("MsgID") %> </ItemTemplate> </asp:TemplateField>
-
I have add check box in a gridview in eachh row at run time.I have a delete button on page.If it is clicked then all checkbox are checed if true yhen delete corresponding row.I have use Template column for checkbox.and bind an id with it.On running page this id is also visible but i want to do invisible it.also how can i retrieve this id if checkbox is checked. here is code... protected void Page_Load(object sender, EventArgs e) { query="select brok.*,cust.FirstName+' '+cust.LastName as Name from brokermsgrecieved as brok,custdetails as cust where brok.BrokerId='"+Session["broker"]+"' and cust.CustId=brok.FromCustId"; dt = obj.select(query); GridView1.DataSource=dt; GridView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { CheckBox chk = new CheckBox(); e.Row.Cells[0].Controls.Add(chk); if (e.Row.Cells[1].Text == "True") { e.Row.BackColor = Color.Wheat; } if (e.Row.Cells[1].Text == "False") { e.Row.BackColor = Color.WhiteSmoke; } } protected void Button1_Click(object sender, EventArgs e) { GridView1.DataSource = dt; GridView1.DataBind(); Int16 count =(Int16) GridView1.Rows.Count; for (Int16 i = 1; i < count; i++) { CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].Controls[1]; if (chk.Checked.Equals(true)) { //query="Delete from brokermsgrecieved where " //GridView1.Rows[i].Cells[0].v query = "delete from brokermsgrecieved where MsgId='1'"; obj.insert(query); } } } here is code such that i bind id---- <Columns> asp:TemplateField <ItemTemplate> <%# Eval("MsgID") %> </ItemTemplate> </asp:TemplateField>
use the HTML name attributre as place holder for other value.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { CheckBox chk = new CheckBox(); // use name html attribute as your ID placeholder chk.Attributes.Add("name", e.Row.Cells[100].Text); //e.Row.Cells[100].Text is my column id e.Row.Cells[0].Controls.Add(chk); } protected void Button1_Click(object sender, EventArgs e) { Int16 count = (Int16)GridView1.Rows.Count; for (Int16 i = 1; i < count; i++) { CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].Controls[1]; //get the value of attribute name. string id = CheckBox3.Attributes["name"]; } }
happy coding.. -
use the HTML name attributre as place holder for other value.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { CheckBox chk = new CheckBox(); // use name html attribute as your ID placeholder chk.Attributes.Add("name", e.Row.Cells[100].Text); //e.Row.Cells[100].Text is my column id e.Row.Cells[0].Controls.Add(chk); } protected void Button1_Click(object sender, EventArgs e) { Int16 count = (Int16)GridView1.Rows.Count; for (Int16 i = 1; i < count; i++) { CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[0].Controls[1]; //get the value of attribute name. string id = CheckBox3.Attributes["name"]; } }
happy coding..