Need GridView Help

Need GridView Help

am 24.01.2008 06:24:06 von 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.

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

Re: Need GridView Help

am 24.01.2008 06:49:08 von 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 data = new List();
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".

Re: Need GridView Help

am 24.01.2008 09:45:02 von Hosmerica

"Jonathan Wood" wrote in message
news:eXe3ZlkXIHA.3940@TK2MSFTNGP05.phx.gbl...
> 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.

Re: Need GridView Help

am 24.01.2008 10:17:06 von mark

"Jonathan Wood" wrote in message
news:eXe3ZlkXIHA.3940@TK2MSFTNGP05.phx.gbl...

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


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Re: Need GridView Help

am 24.01.2008 18:38:29 von 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 data = new List();
> 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.

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

Re: Need GridView Help

am 24.01.2008 18:39:48 von 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.

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

"Hosmerica" wrote in message
news:KPidnYejUquTzQXanZ2dnUVZ_rqlnZ2d@comcast.com...
>
> "Jonathan Wood" wrote in message
> news:eXe3ZlkXIHA.3940@TK2MSFTNGP05.phx.gbl...
>> 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.
>

Re: Need GridView Help

am 24.01.2008 18:43:18 von 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.

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

Re: Need GridView Help

am 24.01.2008 19:30:19 von mark

"Jonathan Wood" wrote in message
news:%23lyidCrXIHA.4880@TK2MSFTNGP03.phx.gbl...

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


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Re: Need GridView Help

am 24.01.2008 19:39:28 von Scott Roberts

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

Purely out of curiosity - what details?

RE: Need GridView Help

am 24.01.2008 20:23:55 von mily242

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:

AutoGenerateColumns="false">





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


"Jonathan Wood" wrote:

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

Re: Need GridView Help

am 24.01.2008 20:49:52 von Jonathan Wood

Scott,

>>> 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.
>
> Purely out of curiosity - what details?

Well, I'm still working that out.

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

Re: Need GridView Help

am 24.01.2008 20:54:22 von Jonathan Wood

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

"Milosz Skalecki [MCAD]" wrote in message
news:BCF0162D-6475-426C-964A-8FE8C0B09C53@microsoft.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:
>
> > AutoGenerateColumns="false">
>
>
>

>

>
> 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
>
>
> "Jonathan Wood" wrote:
>
>> 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
>>
>>

Re: Need GridView Help

am 24.01.2008 20:55:54 von 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?
--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Milosz Skalecki [MCAD]" wrote in message
news:BCF0162D-6475-426C-964A-8FE8C0B09C53@microsoft.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:
>
> > AutoGenerateColumns="false">
>
>
>

>

>
> 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
>
>
> "Jonathan Wood" wrote:
>
>> 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
>>
>>

Re: Need GridView Help

am 24.01.2008 20:58:03 von 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.

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

"Mark Rae [MVP]" wrote in message
news:%23b8nycrXIHA.1132@TK2MSFTNGP06.phx.gbl...
> "Jonathan Wood" wrote in message
> news:%23lyidCrXIHA.4880@TK2MSFTNGP03.phx.gbl...
>
>> 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...
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net

Re: Need GridView Help

am 24.01.2008 22:14:36 von mily242

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" wrote:

> 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
>
> "Milosz Skalecki [MCAD]" wrote in message
> news:BCF0162D-6475-426C-964A-8FE8C0B09C53@microsoft.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:
> >
> > > > AutoGenerateColumns="false">
> >
> >
> >

> >

> >
> > 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
> >
> >
> > "Jonathan Wood" wrote:
> >
> >> 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
> >>
> >>
>
>

Re: Need GridView Help

am 24.01.2008 22:24:10 von Jonathan Wood

Right, I suspected they might be the same things.

Thanks.

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

"Milosz Skalecki [MCAD]" wrote in message
news:D18B28EA-E77C-4337-AC9D-B413DD0A2315@microsoft.com...
> 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" wrote:
>
>> 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
>>
>> "Milosz Skalecki [MCAD]" wrote in message
>> news:BCF0162D-6475-426C-964A-8FE8C0B09C53@microsoft.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:
>> >
>> > >> > AutoGenerateColumns="false">
>> >
>> >
>> >

>> >

>> >
>> > 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
>> >
>> >
>> > "Jonathan Wood" wrote:
>> >
>> >> 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
>> >>
>> >>
>>
>>

Re: Need GridView Help

am 25.01.2008 01:16:19 von mily242

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" wrote:

> Right, I suspected they might be the same things.
>
> Thanks.
>
> --
> Jonathan Wood
> SoftCircuits Programming
> http://www.softcircuits.com
>
> "Milosz Skalecki [MCAD]" wrote in message
> news:D18B28EA-E77C-4337-AC9D-B413DD0A2315@microsoft.com...
> > 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" wrote:
> >
> >> 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
> >>
> >> "Milosz Skalecki [MCAD]" wrote in message
> >> news:BCF0162D-6475-426C-964A-8FE8C0B09C53@microsoft.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:
> >> >
> >> > > >> > AutoGenerateColumns="false">
> >> >
> >> >
> >> >

> >> >

> >> >
> >> > 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
> >> >
> >> >
> >> > "Jonathan Wood" wrote:
> >> >
> >> >> 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
> >> >>
> >> >>
> >>
> >>
>
>

Re: Need GridView Help

am 25.01.2008 01:17:14 von mily242

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" wrote:

> Right, I suspected they might be the same things.
>
> Thanks.
>
> --
> Jonathan Wood
> SoftCircuits Programming
> http://www.softcircuits.com
>
> "Milosz Skalecki [MCAD]" wrote in message
> news:D18B28EA-E77C-4337-AC9D-B413DD0A2315@microsoft.com...
> > 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" wrote:
> >
> >> 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
> >>
> >> "Milosz Skalecki [MCAD]" wrote in message
> >> news:BCF0162D-6475-426C-964A-8FE8C0B09C53@microsoft.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:
> >> >
> >> > > >> > AutoGenerateColumns="false">
> >> >
> >> >
> >> >

> >> >

> >> >
> >> > 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
> >> >
> >> >
> >> > "Jonathan Wood" wrote:
> >> >
> >> >> 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
> >> >>
> >> >>
> >>
> >>
>
>