473,221 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

Adding a select button to my datagrid

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
2 2405
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Clayton Hamilton | last post by:
I have a DataGrid on a webform bound to a Datasource and can successfully use <ItemTemplate> to create edit/update/cancel functionality for user maintenance of data. I use separate logic to delete...
5
by: Phil Townsend | last post by:
I need to add a button to a datagrid. I have tried using the ButtonColumn and have also tried adding a button to a templatecolumn > itemtemplate. Whatever I have tried doesn't work, nor does it...
4
by: Chris | last post by:
I think I must be going crazy. All I want to do is add a button to the bottom of a datagrid which will later run some code for a web based project. I've created a custom control that inherits from...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest project has me kinda stumped and after a couple...
0
by: sonic | last post by:
hello, I have this custom datagrid control, that inherits from DataGrid. In addition to datagrid, I am adding a "status" table under it, and another html button, which will trigger a new row. ...
1
by: Srinivasa Rao | last post by:
I have binded the two datagrids from database from two different tables. In both datagrids, the first column contains checkbox. In between the datagrid, i have "Add -->" and "Remove -->" buttons in...
1
by: John Hughes | last post by:
Im adding a button at runtime to a datagrid but the DataGrid1.ItemCommand event does not fire. I also have a button that I added at design time and this button does fire the event ItemCommand. ...
3
by: Silvia | last post by:
Hi, I have a form and I add buttons programmatically in de form_Load function. Anybody know how implements de function button_click if in the designer mode the button doesn't exists? I...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.