Adding a select button to my datagrid

B

Bob Hollness

Hi group. I am a newbie to ASP.NET as you will see from some of the
questions I may ask!

I have a datagrid which I have populated from a database. It works great!
I have added a column, via the Columns dialog box from the properties of the
datagrid, on the left that contains a select button. So now, when you press
the button the whole row is highlighted.

Now, what I want to do is have the user highlight as many rows as they wish
and then press a button which will process what they have selected.
However, when the user selects a different row, the previous selected one is
unhighlighted.

1.) How can I have it so that the user can select multiple rows?
2.) If I wanted to change it from a button to a checkbox, how is this
possible?

Thanks.
 
G

Guest

By default, the DataGrid control allows you to selected only one row.

Several possibilities for your problem :

1 / Create an ArrayList that will be stored into the Viewstate, this
arrayList will contains the selected row index.

protected ArrayList SelectedItems
{
get
{
ArrayList selectedItems = (ArrayList) ViewState["SelectedItems"];
if (selectedItems == null)
{
selectedItems = new ArrayList();
ViewState["SelectedItems"] = selectedItems;
}
return selectedItems;
}
set
{
ViewState["SelectedItems"] = value;
}
}

2 / Add a Command Button into your dataGrid with the command name
CustomSelect :

<asp:buttoncolumn text="Select" commandname="CustomSelect"></asp:buttoncolumn>

3 / Handle the ItemCommand event of the DataGrid, and write the following to
save the item index into the arraylist.

private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
e.Item.BackColor = System.Drawing.Color.Red;
ArrayList selectedItems = SelectedItems;
if (selectedItems.Contains(e.Item.ItemIndex) == false)
selectedItems.Add(e.Item.ItemIndex);

}
}

4/ Now you can use the SelectedItems arraylist to retreives the selected
items from the DataGrid.

The checkbox is possible by adding a template column to you DataGrid like
that :

<templateColumn>
<itemtemplate>
<asp:checkbox id="Checkbox1" runat="server" />
</itemtemplate>
</templateColumn>
 
B

Bob Hollness

Cyril said:
By default, the DataGrid control allows you to selected only one row.

Several possibilities for your problem :

1 / Create an ArrayList that will be stored into the Viewstate, this
arrayList will contains the selected row index.

protected ArrayList SelectedItems
{
get
{
ArrayList selectedItems = (ArrayList) ViewState["SelectedItems"];
if (selectedItems == null)
{
selectedItems = new ArrayList();
ViewState["SelectedItems"] = selectedItems;
}
return selectedItems;
}
set
{
ViewState["SelectedItems"] = value;
}
}

2 / Add a Command Button into your dataGrid with the command name
CustomSelect :

<asp:buttoncolumn text="Select"
commandname="CustomSelect"></asp:buttoncolumn>

3 / Handle the ItemCommand event of the DataGrid, and write the following
to save the item index into the arraylist.

private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
e.Item.BackColor = System.Drawing.Color.Red;
ArrayList selectedItems = SelectedItems;
if (selectedItems.Contains(e.Item.ItemIndex) == false)
selectedItems.Add(e.Item.ItemIndex);

}
}

4/ Now you can use the SelectedItems arraylist to retreives the selected
items from the DataGrid.

The checkbox is possible by adding a template column to you DataGrid like
that :

<templateColumn>
<itemtemplate>
<asp:checkbox id="Checkbox1" runat="server" />
</itemtemplate>
</templateColumn>


Thanks! I'll try it and post back.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top