Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid checkbox asp.net c# 1

Status
Not open for further replies.

ludmann

Programmer
Apr 14, 2004
49
GB
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
 
Here's what I used that get the index number of the rows that is checked, creates arraylist, and passed into a querystring


Sub OnNext (sender as object, e as EventArgs)
Dim selectedRows as ArrayList
selectedRows = new ArrayList()
Dim aitem as DataGridItem
for each aitem In DataGrid1.Items
Dim chkBox as CheckBox
chkBox = aitem.FindControl("selectRow")
if (chkBox.Checked)
dim id as Textbox
id = aitem.FindControl("ID")
selectedRows.Add(ID.Text)
end if
next aitem
if (selectedRows.Count > 0)
dim nextURL as Stringbuilder
nextURL = new StringBuilder ("printquote.aspx?")
Dim productID as string
for each productID in selectedRows
nextURL.Append("ID=")
nextURL.Append(productID)
nextURL.Append("&")
next productID
Response.Redirect(nextURL.ToString())
End if
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top