Groups | Blog | Home
all groups > asp.net > january 2008 >

asp.net : Need GridView Help


Jonathan Wood
1/23/2008 10:24:06 PM
I'm displaying data in a GridView that allows user to select a row. When a
button is clicked, I need to locate the selected row.

The problem is that I need an ID value from the selected row, but I do not
want to display that ID value in the grid. I tried creating a hidden column,
but I can't seem to be able to access it when the button is clicked. (At
least, hidden columns do not appear to show up in the Cells collection of
the selected row.)

Is there any way to get an ID for the selected row without displaying that
ID to the user?

Also, does anyone know if there's any way to populate a GridView control
without using the DataSource property, by just programatically adding rows
to the grid?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Scott Roberts
1/23/2008 11:49:08 PM
[quoted text, click to view]

Look into the DataKeyNames and DataKeys properties.

[quoted text, click to view]

I don't know if you can "just add rows" or not, but I do know that you can
bind a GridView to pretty much anything (I don't know the specific
interface(s) off the top of my head). So, you can just create a list of
objects (for example) and bind that:

List<MyObject> data = new List<MyObject>();
PopulateData(data);
MyGridView.DataSource = data;
MyGridView.DataBind();

MyObject can be any class with public properties. So each object instance is
a "row" and each public property is (potentially) a "column". IMO, it's even
easier than adding rows & columns "manually".
Hosmerica
1/24/2008 3:45:02 AM

[quoted text, click to view]

Have you tried using CommandArgument and/or CommandName? The
CommandArgument can be assigned to the button as can the CommandName. When
you click the button, you'd have to catch it in the RowCommand Event. You
can do your processing from there.

Mark Rae [MVP]
1/24/2008 9:17:06 AM
[quoted text, click to view]

When the data is bound to the GridView, populate the button's
CommandArgument property with the ID...

However, there's no need to use a button if all you want is the user to be
able to select a row:

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Style["cursor"] = "pointer";
e.Row.Attributes.Add("onclick",
ClientScript.GetPostBackEventReference(MyGridView, "Select$" +
e.Row.RowIndex.ToString()));
}
}

protected void MyGridView_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("OtherPage.aspx?ID=" +
MyGridView.SelectedValue.ToString(), false);
}


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Jonathan Wood
1/24/2008 10:38:29 AM
Scott,

[quoted text, click to view]

Okay, I saw those but they seemed more database related. I see what they do
now.

[quoted text, click to view]

Yeah, I'm actually doing this now. It's definitely easier than adding rows
manually, but it doesn't provide as much control over some details.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jonathan Wood
1/24/2008 10:39:48 AM
I haven't tried that, no. But I'll look into it.

One problem is that I don't do any processing when the row is selected. But
then I have some buttons that are not part of the grid. When they are
clicked, then I need to be able to locate the ID of the selected item.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

[quoted text, click to view]
Jonathan Wood
1/24/2008 10:43:18 AM
Mark,

[quoted text, click to view]

Yeah, I'll play around with that. But, like I mentioned elsewhere in this
thread, I don't do any processing when the item is selected. I need to be
able to access the ID of the selected row when a button outside the grid is
pressed. It sounds like this is mostly geared towards handling row
events--unless there's a bit more to this.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Milosz Skalecki [MCAD]
1/24/2008 11:23:55 AM
Jonathan,

No need to write custom code (like setting commandargument) as GridView has
a built in mechanism of handling such scenario, already mentioned by Scott.
In addition you can use currently selected ID wherever you like:

<asp:GridView runat="server" ID="gv" DataKeyNames="IDFieldOrIDColumn"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="WhateEver" />
</Columns>
</asp:GridView>

in the code you can simply write:

if (gv.SelectedValue != null)
{
// i assume record id is integer
int recordId = (int) gv.SelectedValue;
}

No need for writing custom code
--
Milosz


[quoted text, click to view]
Scott Roberts
1/24/2008 12:39:28 PM

[quoted text, click to view]

Purely out of curiosity - what details?
Jonathan Wood
1/24/2008 12:49:52 PM
Scott,

[quoted text, click to view]

Well, I'm still working that out. <g>

Some things I'm looking at including only showing the value in one column
when it is different from the last. Also, I'd like to alternate row colors
based on when this same value changes. I'm not 100% certain if there's
anything else I need.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Jonathan Wood
1/24/2008 12:54:22 PM
Assuming you meant gv.SelectedDataKey.Value (instead of gv.SelectedValue)
then, yeah, that should do what I want.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

[quoted text, click to view]
Jonathan Wood
1/24/2008 12:55:54 PM
Er... no, I guess you did mean SelectedValue. Although, I'm not clear on the
difference between SelectedDataKey.Value and SelectedValue. Are they the
same thing?
--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

[quoted text, click to view]
Jonathan Wood
1/24/2008 12:58:03 PM
I'll have to take your word for it. I can't seem to see how CommandArgument
could be made to work for my purposes.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

[quoted text, click to view]
Milosz Skalecki [MCAD]
1/24/2008 1:14:36 PM
Hi Jonathan,

SelectedValue is a shortcut for SelectedDataKey.Value which is a wrapper
around
DataKeyArray[SelectedIndex] and SelectedDataKey.Values[0]. It matters only
if you use several columns in DataKeyNames (which i don't think is the case
now?).

Regards,
--
Milosz


[quoted text, click to view]
Jonathan Wood
1/24/2008 2:24:10 PM
Right, I suspected they might be the same things.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

[quoted text, click to view]
Milosz Skalecki [MCAD]
1/24/2008 4:16:19 PM
Hi there again,

Sorry i wasn't clear. In most scenarios, you need just one column to
identify the row (i.e. UserId - usually identity column automatically
incremented). Now, in order to simplify the code, it's easier to use just
gridView.SelectedValue than gridView.SelectedDataKey.Values[0]. But sometimes
one column is not enough, or you want to store more values from the record
per each row, i.e. you want UserId, Login, LastLoginDate information to be
available for each record:

int userId = (int) gridView.SelectedDataKey.Values[0];
string login = (string) gridView.SelectedDataKey.Values[1];
DateTime lastLoginDate = (DateTime) gridView.SelectedDataKey.Values[2];

Hope it's clear now :)

Regards
--
Milosz


[quoted text, click to view]
Milosz Skalecki [MCAD]
1/24/2008 4:17:14 PM
Sorry i wasn't clear. In most scenarios, you need just one column to identify
the row (i.e. UserId - usually identity column automatically incremented).
Now, in order to simplify the code, it's easier to use just
gridView.SelectedValue than gridView.SelectedDataKey.Values[0]. But sometimes
one column is not enough, or you want to store more values from the record
per each row, i.e. you want UserId, Login, LastLoginDate information to be
available for each record:

int userId = (int) gridView.SelectedDataKey.Values[0];
string login = (string) gridView.SelectedDataKey.Values[1];
DateTime lastLoginDate = (DateTime) gridView.SelectedDataKey.Values[2];

Hope it's clear now :)

Regards
--
Milosz


[quoted text, click to view]
Mark Rae [MVP]
1/24/2008 6:30:19 PM
[quoted text, click to view]

In that case, the CommandArgument property is definitely the way to go...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
AddThis Social Bookmark Button