473,325 Members | 2,712 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,325 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 2410
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.