By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

Adding a select button to my datagrid

P: n/a
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.
--

Bob

--------------------------------------
I'll have a B please Bob.
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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>
"Bob Hollness" wrote:
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.
--

Bob

--------------------------------------
I'll have a B please Bob.

Nov 19 '05 #2

P: n/a
Cyril S. wrote:
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.
--

I'll have a B please Bob.
Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.