I am adding Select Checkbox at every row in GridView while loading from database. Below is my aspx page code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="100%" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdatabound="GridView1_RowDataBound" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%# Eval("ID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Part Number">
<ItemTemplate>
<asp:Label ID="lblpn" runat="server" Text='<%# Eval("PartNumber") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Part Desc">
<ItemTemplate>
<asp:Label ID="lblpd" runat="server" Text='<%# Eval("PartDesc") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I want to read the data from the rows which are selected in the respective Checkbox. For this I have done following code in Button Click:
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow dr = GridView1.Rows[i];
Label lblID = (Label)dr.FindControl("lblid");
ID = Convert.ToInt32(lblID.Text);
CheckBox checkBox = (CheckBox)dr.FindControl("cbSelect");
if (checkBox.Checked)
{
}
}
Now if I select single row in grid then I am able to get the checkbox status Checked for that particular row.
But If I select multiple rows then I am getting only first selected row's checkbox status as checked. From second row I am getting Check status as Unchecked. But I am able to get correct value in lblID label control.
Following is my function to fill gridview:
public void showgrid(string Location)
{
DataTable dt = new DataTable();
con.Open();
string strQuery = "";
SqlDataAdapter sda = new SqlDataAdapter();
if (chkConsiderPart.Checked)
strQuery = "select * from InventoryDetails where Status='Stocked' and ToLocation='" + Location + "' and PartNumber='" + ddlPartNumber.SelectedItem.Text + "'";
else
strQuery = "select * from InventoryDetails where Status='Stocked' and ToLocation='" + Location + "'";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
if (dt.Rows.Count == 0)
{
lblRowsStatus.Text = "No Records found for the selection.";
}
else
{
lblRowsStatus.Text = "There are " + dt.Rows.Count.ToString() + " Records found for the selection.";
}
}
I am only calling it in:
if (!IsPostBack)
{
showgrid(ddlFromLocation.SelectedItem.Text);
}
Also there was post-back at certain events of other controls. but even if I removed them same issue is happening.
Note: There is no code written in any of Gridview Events.
Aucun commentaire:
Enregistrer un commentaire