Need GridView Help

J

Jonathan Wood

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.
 
S

Scott Roberts

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

Look into the DataKeyNames and DataKeys properties.
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?

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".
 
H

Hosmerica

Jonathan Wood said:
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.)

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.
 
M

Mark Rae [MVP]

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

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);
}
 
J

Jonathan Wood

Scott,
Look into the DataKeyNames and DataKeys properties.

Okay, I saw those but they seemed more database related. I see what they do
now.
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".

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.
 
J

Jonathan Wood

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.
 
J

Jonathan Wood

Mark,
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);
}

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.
 
M

Mark Rae [MVP]

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.

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

Scott Roberts

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.

Purely out of curiosity - what details?
 
M

Milosz Skalecki [MCAD]

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
 
J

Jonathan Wood

Scott,
Purely out of curiosity - what details?

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.
 
J

Jonathan Wood

Assuming you meant gv.SelectedDataKey.Value (instead of gv.SelectedValue)
then, yeah, that should do what I want.

Thanks.
 
J

Jonathan Wood

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?
 
J

Jonathan Wood

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.
 
M

Milosz Skalecki [MCAD]

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,
 
J

Jonathan Wood

Right, I suspected they might be the same things.

Thanks.

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

Milosz Skalecki said:
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


Jonathan Wood said:
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?
 
M

Milosz Skalecki [MCAD]

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


Jonathan Wood said:
Right, I suspected they might be the same things.

Thanks.

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

Milosz Skalecki said:
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


Jonathan Wood said:
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

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


:

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.
 
M

Milosz Skalecki [MCAD]

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


Jonathan Wood said:
Right, I suspected they might be the same things.

Thanks.

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

Milosz Skalecki said:
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


Jonathan Wood said:
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

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


:

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.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top