C# - ASP - Gridview

G

Guest

Hello,
I have a gridview control bound to a objectdatasource.
The primary key column is on the grid but made visible = false.
Because the column is invisible I cant get to the cell value when the user
presses
the edit button on the grid.
Code

protected void grdBedpDetails_RowEditing(object sender,
GridViewEditEventArgs e)
{

String bedpID = grdBedpDetails.Rows[e.NewEditIndex].Cells[1].Text;
}

I have set up the grdBedpDetails.DataKeyNames property
but this is only useful in the selectedindex event not the RowEditing event

grdBedpDetails.Rows[e.NewEditIndex].Cells[1].Text return a empty string
because the column is invisible.

Thanks
 
W

Walter Wang [MSFT]

Hi,

Thank you for your post!

You have two options to get the hidden columns' values depending on whether
or not the columns belong to the DataKeyNames property of GridView.

1) If the hidden columns are included in DataKeyNames:

int id = (int) grdBedpDetails.DataKeys[e.NewEditIndex].Values[0];

2) If the hidden columns are not included in DataKeyNames:

Since hidden columns are not rendered to the client, you need to hide the
columns using CSS rule instead of setting its property Visible="false".
Below are the detailed steps:

a) Create a CSS rule in html header:

<style type="text/css">
.hidden { display: none; }
</style>

b) Hide a column by applying CSS class to its header and item:

<asp:BoundField DataField="ID" HeaderStyle-CssClass="hidden"
ItemStyle-CssClass="hidden" />

c) Then you can get the ID column text by:

String bedpID = grdBedpDetails.Rows[e.NewEditIndex].Cells[1].Text;


Hope this helps. If anything is unclear, please feel free to post here.



Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Walter,
Thanks for your reply
number 1) work great you saved my day again !!

I have another question releated to the same gridview , I was hoping you
have a easy answer
I have column 0 = CommandField ( view)
I have column 1 = CommandFiled ( edit)

When the event grdTemp_SelectedIndexChanged
occurs on the server I want to know which column was selected
eg do somthing when view pressed do somthing else when edit pressed.

Thanks in advance
 
M

Mark Rae

Since hidden columns are not rendered to the client, you need to hide the
columns using CSS rule instead of setting its property Visible="false".
Below are the detailed steps:

Much easier simply to unhide the columns temporarily, bind the data and then
hide the columns again e.g.

MyGridView.Columns[n].Visible = true;
MyGridView.DataBind();
MyGridView.Columns[n].Visible = false;

Saves all that unnecessary messing about with CSS.
 
G

Guest

Hello Mark,
Sounds like a good work around.
But it didnt work for me
The cell returned empty in the event

grdBedpDetails_RowEditin


grdBedpDetails.Columns[6].Visible = true;
grdBedpDetails.DataKeyNames = dataKeyNames ;
grdBedpDetails.DataSourceID = "ODSBedps";
ODSBedps.SelectParameters.Clear();
ODSBedps.SelectMethod = "GetBedpList";
grdBedpDetails.DataBind();
grdBedpDetails.Columns[6].Visible = false;

--
Life in the sun


Mark Rae said:
Since hidden columns are not rendered to the client, you need to hide the
columns using CSS rule instead of setting its property Visible="false".
Below are the detailed steps:

Much easier simply to unhide the columns temporarily, bind the data and then
hide the columns again e.g.

MyGridView.Columns[n].Visible = true;
MyGridView.DataBind();
MyGridView.Columns[n].Visible = false;

Saves all that unnecessary messing about with CSS.
 
M

Mark Rae

Sounds like a good work around.
But it didnt work for me
The cell returned empty in the event

Strange - it has always worked for me. Can you try to set the column's
visibility to true *immediately* before doing the bind e.g.

grdBedpDetails.Columns[6].Visible = true;
grdBedpDetails.DataBind();
grdBedpDetails.Columns[6].Visible = false;
 
W

Walter Wang [MSFT]

Hi,

Thank you for your update!

I've seen you opened a new post regarding this problem in this newsgroup. I
will reply in that thread. Thank you for your understanding.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top