I bound gridview with databse..property Checkbox -true .
I have three columns roll,name ,roll.
I want that on button_click, roll nos are displayed whose checkboxes are checked in gridview--
SOURCE TAB
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField >
<ItemTemplate >
<asp:CheckBox ID ="chk" runat ="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Roll" DataField ="roll" />
<asp:BoundField HeaderText="Name" DataField ="name"/>
<asp:BoundField HeaderText="Marks" DataField ="marks"/>
</Columns>
</asp:GridView>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField >
<ItemTemplate >
<asp:CheckBox ID ="chk" runat ="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Roll" DataField ="roll" />
<asp:BoundField HeaderText="Name" DataField ="name"/>
<asp:BoundField HeaderText="Marks" DataField ="marks"/>
</Columns>
</asp:GridView>
public partial class Default3 : System.Web.UI.Page
{
string query;
SqlConnection conn = new SqlConnection("Data Source=SQLEXPRESS;Initial catalog=mansi;Integrated Security=true");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
query = "Select * from Information";
conn.Open();
cmd = new SqlCommand(query, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds=new DataSet ();
da.Fill (ds );
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
bool bchecked;
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
bchecked = ((CheckBox)GridView1.Rows[i].FindControl("chk")).Checked;
}
}
}
Suppose i have three rows in Gridview & I Uncheck first & third checkboxes,& check the second checkbox,
But everytime in varable bchecked false is coming...y so???Plz help me out..