GridView Question

R

Rob Meade

Hi all,

In the past I've typically steered away from DataGrids and have just created
my own tables by programmatically adding rows/cells however yesterday I used
a DataView for the first time. Initially I was quite pleased with the
quickness in getting it to populate with my own collection class and display
results to the page.

There were some oddities...

1. If I get 5 columns and set one of them to have Visible=False (as I want
the value from the data but dont want it displayed) the value comes through
as "", the only way I could get around this was to set Visible = False in my
code after I'd got the value (e.Row.Cells(4) etc etc) as opposed to setting
the same attribute in the source view on the page. A colleague mentions you
used to be able to do this with a DataGrid in .Net 1.1 and you could still
access the value?

2. I was hoping that for each row of data that I had I would be able to
"display" two rows, the first would have a couple of data items, and then
the second row would display one data item which is a "summary" and thus
contains quite a lot of text. I've tried several things but haven't yet
found a way to do this. Only ideas really seem to be to have only ONE cell
displayed and then create my own table inside it, that seems a bit daft as I
might as well just create my own table and save on the code written to the
page. Is there anyway to use some form of template like you can with a
repeater control? The only reason I'm still investigating this is primarily
because I would appreciate the "free" functionality of paging on the
DataView as opposed to having to write my own for my own table code.

Any help/advise would be really appreciated.

Regards

Rob
 
R

Ross Culver

Rob,

Make your key value invisible. Then add it to your DataKeyNames list with
your DataMember = DefaultView. The in your code you can access that key
value using the selecteddatakey.value of the gridview or
gridview.Datakeys.item(index).value property.
Ross
 
J

Joey

Hi all,

In the past I've typically steered away from DataGrids and have just created
my own tables by programmatically adding rows/cells however yesterday I used
a DataView for the first time. Initially I was quite pleased with the
quickness in getting it to populate with my own collection class and display
results to the page.

There were some oddities...

1. If I get 5 columns and set one of them to have Visible=False (as I want
the value from the data but dont want it displayed) the value comes through
as "", the only way I could get around this was to set Visible = False in my
code after I'd got the value (e.Row.Cells(4) etc etc) as opposed to setting
the same attribute in the source view on the page. A colleague mentions you
used to be able to do this with a DataGrid in .Net 1.1 and you could still
access the value?

2. I was hoping that for each row of data that I had I would be able to
"display" two rows, the first would have a couple of data items, and then
the second row would display one data item which is a "summary" and thus
contains quite a lot of text. I've tried several things but haven't yet
found a way to do this. Only ideas really seem to be to have only ONE cell
displayed and then create my own table inside it, that seems a bit daft as I
might as well just create my own table and save on the code written to the
page. Is there anyway to use some form of template like you can with a
repeater control? The only reason I'm still investigating this is primarily
because I would appreciate the "free" functionality of paging on the
DataView as opposed to having to write my own for my own table code.

Any help/advise would be really appreciated.

Regards

Rob

I just finished upgrading an asp.net 1.1 application to asp.net 2.0.
The app had lots of datagrids, and I had been using visible=false on
many of the columns. I would have to say that I agree with Ross about
DataKeyNames. When you use them, for each row in the data source the
specified value (i.e. RoleId, etc...) is added into the grid. Just add
your attribute name (or names separated by commas) to the
DataGridView's DataKeyNames property. You can then access it in code
like this...

protected void grdMyGridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if(this.grdMyGridView.DataKeys[e.Row.RowIndex].Values[0].ToString()
== "SomeValue")
{
//execute some code
}
}

In the above example if you have used more than one datakey, you could
access the second with .Values[1].ToString() instead
of .Values[0].ToString() etc...

Also, I too used to manually construct grids with code by using table,
tr, td and other tags. Once I realized how to tap into the various
available events for grids (i.e. RowDataBound, RowDeleting, etc...), I
quickly began to see the err of my ways!

Finally, to get your two rows you may choose to access the cell within
a RowDataBound or other event handler and then embed text for a table
(with two rows) as the cell data. You can tell the gridview to allow
your HTML text by setting the column's HTMLEncode property to False.

HTH,
JP
}
 
R

Ross Culver

Have you tried converting the column field to a template field? Once you've
done that, you can do all kinds of cool things.

Ross
 

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

Similar Threads

GridView Question 1
Question about my projects 3
GridView Question 0
GridView 0
sorting gridview question 6
Pythen question 0
GridView 13
Grouping messages that have similarities 2

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top