I am using a datagrid where the first column consists of check boxes and the second column is bound to a database table.
<asp:datagrid>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox id="chkSelect" runat="server" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="name" HeaderText="Name"></asp:BoundColumn>
</Columns>
</asp:datagrid>
When I press the submit button, I want to save the selected names (selected via the checkbox) in some sort of a format (like ArrayList or Vector)
Has anyone got any examples of such?
Also, so far I just wanted to check if the selection works and if I can actually count the number of selected rows.
So I used the the following code:
private void Submit_Button_Click(object sender, System.EventArgs e)
{
int rowCount = 0;
foreach(DataGridItem resourcesDataGridItem in resourcesDataGrid.Items)
{
CheckBox myCheckbox = (CheckBox)resourcesDataGridItem.Cells[0].Controls[1];
if(myCheckbox.Checked == true)
{
rowCount++;
}
}
resultlbl.Text = rowCount.ToString();
}
Unfortunately it does not seem to work as it resets to 0 each time. If I mark the checkbox in the datagrid as Checked then each time it will return the number of check boxes (because they are all selected)
What am I doing wrong?
Marika
<asp:datagrid>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox id="chkSelect" runat="server" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="name" HeaderText="Name"></asp:BoundColumn>
</Columns>
</asp:datagrid>
When I press the submit button, I want to save the selected names (selected via the checkbox) in some sort of a format (like ArrayList or Vector)
Has anyone got any examples of such?
Also, so far I just wanted to check if the selection works and if I can actually count the number of selected rows.
So I used the the following code:
private void Submit_Button_Click(object sender, System.EventArgs e)
{
int rowCount = 0;
foreach(DataGridItem resourcesDataGridItem in resourcesDataGrid.Items)
{
CheckBox myCheckbox = (CheckBox)resourcesDataGridItem.Cells[0].Controls[1];
if(myCheckbox.Checked == true)
{
rowCount++;
}
}
resultlbl.Text = rowCount.ToString();
}
Unfortunately it does not seem to work as it resets to 0 each time. If I mark the checkbox in the datagrid as Checked then each time it will return the number of check boxes (because they are all selected)
What am I doing wrong?
Marika