Asked by:
gridview column w/visible=false no longer contains cell data in beta 2

Question
-
User300685930 posted
If you make a simple gridview with 2 columns and make both columns visible, you cn use the code below to get the value. When you set visible="false" in the .aspx file, the value is no longer populated and str0 returns an empty string.
So, this did retrieve the data in beta 1 but does not now. How can I force it to retrieve the data and now show the value. I need the data for foreign key relations I am managing myself.
thanks, -Peter
protected
void GridView1_SelectedIndexChanged(object sender, EventArgs e)GridView
gv = (GridView)sender; int index = gv.SelectedIndex; GridViewRow gvw = gv.Rows[index]; string str0 = (string)gvw.Cells[0].Text;Monday, April 18, 2005 6:38 PM
All replies
-
User1944777292 posted
For security reasons, we no longer databind fields that aren't visible, as you've noticed. This was because often users thought making a field invisible prevented its contents from getting to the client, when in fact the values were present in ViewState.
If you need a value of a particular field but don't want to display it, add it to your DataKeyNames property and you can access the value through the DataKeys property. This has the added benefit of saving off the real value of the field rather than the ToString() representation of the value.
Wednesday, April 20, 2005 5:20 PM -
User1822298252 posted
I understand your reasons to not databind hidden fields and use DataKeys array instead. But it looks like there is a problem with this approach. DataKeys array doesn't get populated if I use paging and I process postback not from the first page. The scenario looks like this... I load page with a GridView. If I click some row (CommandButton or LinkButton) right away, then DataKeys array gets populated and everything is fine, but if I click on page link first and after page gets reloaded try to click on some row - here we go, DataKeys.Count is 0! Is this a bug?
Wednesday, April 20, 2005 6:00 PM -
User1944777292 posted
Are you setting the DataSource property rather than the DataSourceID property? If so, then yes, it's a bug that's been fixed, but it didn't make it into Beta 2. Subsequent builds will have it. My apologies.Wednesday, April 20, 2005 6:24 PM -
User1822298252 posted
Right, it doesn't work only if DataSource property assgined.
My workaround for this bug is to use custom commands and bind value of id to CommandArgument property.
I described my solution here: http://forums.asp.net/900598/ShowPost.aspx#900598
Wednesday, April 20, 2005 6:36 PM -
User300685930 posted
I'm hoping I can set visible="true" and make the column width 0. I don't need to hide foreign key type values form the viewstate variableWednesday, April 20, 2005 7:02 PM -
User1944777292 posted
Setting table cell width to less than the space it takes to render out its content will not work in most browsers- it will just auto-resize to the smallest size it needs to render your content.
As long as you're using the DataSourceID property instead of the DataSource property, setting DataKeyNames should work fine for you. DataKeyNames was set up exactly to make this scenario easy.
Wednesday, April 20, 2005 8:01 PM -
User-686502552 posted
DataKeyNames works fine for a Select event but I don't think that will work for an Edit event(or does it?). Is there a solution for Edits?...I guess the same question goes for Deletes as well
thanks in advance.
Thursday, May 5, 2005 12:12 AM -
User1944777292 posted
DataKeyNames should work all the time, even for edits and deletes. DataKeys should be populated in the RowUpdating and RowDeleting events. It will also be available in the Keys property in the EventArgs of the two events above. If you don't want those fields in the Keys dictionary passed to the data source, you can remove them from the Keys dictionary in a handler for those events.
Thursday, May 5, 2005 1:42 AM -
User-686502552 posted
My question was bad. What I meant to ask is that in the case of Select events you can use the GridView.SelectedValue to retrieve the selected key value. What would be the equivalent for Edit and Deletes? Also I needed to handle edits in the RowEditing event, RowUpdating is a little too late for my purpose.
Thanks again.
Thursday, May 5, 2005 4:51 PM -
User1944777292 posted
You can use gridView.DataKeys[gridView.EditIndex] to get the data keys for the current edit item. You can use a similar indexing for the RowDeleting event, or you can use the Keys property on the event args.Thursday, May 5, 2005 5:00 PM -
User-686502552 posted
It worked out except instead of using the gridview.EditIndex inside of the RowEditing event i used the GridViewEditEventArgs.NewEditIndex(the EditIndex was evaluating to -1).
Thanks again for the prompt responses.
Thursday, May 5, 2005 5:27 PM -
User2053580692 posted
The problem here is i want to send that data directly to my datasource for a deleteCommand
typically i can take a bound command and just use the @name in my SQL query.
it doesnt appear as though i can do that with the datakeynames.
why didnt you leave it the way it was in 1.1? i dont think you should dumby down things because people dont read how things work. it should be fairly obvious.
JustinThursday, May 26, 2005 11:46 AM -
User300685930 posted
Not sure Iunderstand how to do this. Here is what I try but does not work. str0 returns the id_file, but str1 errors saying: Unable to cast object of type 'System.Int32' to type 'System.String'. the column filedescr is a string in the database
<asp:GridView ID="GridView1" DataKeyNames="id_file,filedescr" DataSourceID="SqlDataSource1" AllowPaging="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
protected
void GridView1_SelectedIndexChanged(object sender, EventArgs e){
GridView gv = (GridView)sender; string str0 = gv.DataKeyNames[0]; int idFile = (int) gv.DataKeys[0].Value; string str1 = (string) gv.DataKeys[1].Value;}
Thursday, May 26, 2005 12:31 PM -
User82929862 posted
Another work around for this is to convert one of your columns to template and add a hidden label field! You can get it from frindcontrolThursday, May 26, 2005 12:34 PM -
User1944777292 posted
Justin- we made this particular change for security reasons. Most people don't realize that the contents of an invisible field are sent to the client in readable format. You should be able to get your field from the DataKey property or in the Keys property in the GridViewDeleteEventArgs. Then you can just move the field to the Values dictionary in the event args. Everything will continue to work.
pkellner- I think this is the code you really want:
GridView gv = (GridView)sender;
string str0 = gv.DataKeyNames[0]; // this will get you the string "id_file"- is that what you want?
int idFile = (int) gv.DataKeys[gv.SelectedIndex].Value;
string str1 = (string) gv.DataKeys[gv.SelectedIndex].Values[1].ToString();
Rafa- Yes, your suggestion will work, but please remember that the contents of the hidden label will be sent to the client.Thursday, May 26, 2005 1:24 PM -
User300685930 posted
thank you, thank you, thank you.
I love answers like this, especially when they work.
-Peter
Thursday, May 26, 2005 3:06 PM -
User1258001072 posted
Are you setting the DataSource property rather than the DataSourceID property? If so, then yes, it's a bug that's been fixed, but it didn't make it into Beta 2. Subsequent builds will have it. My apologies.
So if I'm grabbing a dataset via my data tier and linking it in code using gv.DataSource = ds; or something similar, and I'm also using paging, it's going to error. You're saying this problem has been fixed but didn't make it into beta 2? Is this fix available? Is there some other workaround that allows me to still use DataSource and not DataSourceID?Wednesday, June 1, 2005 4:35 PM -
User300685930 posted
Good thought. Thanks. I'm pretty happy with the daatakeynames solution suggested above. That works very well and makes a lot of sense to me to do it that way.Wednesday, June 1, 2005 4:45 PM -
User1944777292 posted
The bug was that we cleared out the data keys when not bound to a data source control when handling the paging event. You can work around the issue by handing the RowCommand event. If the CommandName is "Page", it's a paging event. The DataKeys property should still be populated in the RowCommand event. Or, you can wait until after you're done paging and call DataBind. In the OnDataBound event, you can retrieve the key values.Wednesday, June 1, 2005 4:49 PM -
User1258001072 posted
ok, thanks for the quick response :DWednesday, June 1, 2005 5:06 PM -
User803728874 posted
It seems then that anytime that values for the row need to be used for logic then the datakeys should be used opposed to accessing the columns. This would eliminate future problems where column order changes or is removed. That being the case, I'm using this when I need to use row data:
string value1 = gdvName.DataKeys[e.Row.RowIndex].Values[0].ToString();
string value2 = gdvName.DataKeys[e.Row.RowIndex].Values[1].ToString();
Is there a performance hit with this?Monday, June 6, 2005 4:42 PM -
User803728874 posted
pkellner,
You need to use Values instead of Value, that should work for you.Monday, June 6, 2005 4:43 PM -
User1944777292 posted
The largest impact of setting DataKeyNames is in the page state- since they're stored in page state, it will increase the size of your page. There is also a small performance hit to evaluating and serializing those data key values. But unless performance is your greatest concern and you're tuning for tenth-of-a-percent performance gains, I wouldn't worry about the perf hit.
The page size hit may be a bigger deal to you. It, like so many other things, is a trade-off. If you're already displaying your datakeys in your grid and can retrieve them easily, then maybe using them straight out of the grid is better for you because your page size will be smaller. If you're not worried about page size as much as stability of the code, setting DataKeyNames and getting the values from DataKeys is a more maintainable way to go. If your columns move around and you need to calculate the datakey column in the grid each time you need a value, using DataKeyNames and DataKeys seems like it will be much less of a headache for the page programmer.
Another note- for your production page, if you decide to use DataKeyNames and you're not autogenerating your grid columns, the GridView will assume your datakeys are sensitive and will ask the page to encrypt its viewstate, which is a perf hit. If you don't need to hide your datakey values from your clients, you can tell the page you don't want viewstate encrypted by setting the page directive ViewStateEncryptionMode to Never instead of Auto. This will save you several percentage points in perf.
Monday, June 6, 2005 6:12 PM -
User300685930 posted
OK, I thought I understood, but on the last post you said that "if you decide to use DataKeyNames..." you implied that they would be stored in the viewstate and would get enrypted? I thought that datakeynames (hidden of course) would not show up in the viewstate so I would not need to worry about the page encrypting?
Sorry, for dragging this already long thread longer.
-PeterMonday, June 6, 2005 6:20 PM -
User1944777292 posted
DataKeys do get stored in page state so they're available on postback. To be precise, they're in ControlState, not ViewState, but for most clients, it's the same difference- it's in a hidden field on the page. If you're using callbacks (post-Beta 2), DataKeys will also get stored in another hidden field on your page because ControlState can't be updated within a callback, and sorting and paging will influence the contents of the DataKeys property.Monday, June 6, 2005 7:46 PM -
User1119954915 posted
DataKeys do get stored in page state so they're available on postback. To be precise, they're in ControlState, not ViewState, but for most clients, it's the same difference- it's in a hidden field on the page.
From earlier...
For security reasons, we no longer databind fields that aren't visible, as you've noticed. This was because often users thought making a field invisible prevented its contents from getting to the client, when in fact the values were present in ViewState.
So i am lost, how is this more secure?Monday, June 13, 2005 8:40 PM -
User1309403994 posted
The index for DataKeys needs to be set to the actual row index, derived from EventArgs.
For example,
int index = gv.SelectedIndex;
string str0 = gv.DataKeys(index);Monday, June 13, 2005 10:22 PM -
User1944777292 posted
If DataKeyNames is set, we request that the page encrypts the contents of ViewState and ControlState. We do this because we know that DataKeyNames is likely to be sensitive. If you have the contents of a sensitive field in an invisible field and we were to databind that field, you would likely not realize that you were exposing that data to all your clients, and we would have a hard time knowing whether we should encrypt that data. So we decided that most everyone really can use DataKeyNames instead of creating invisible fields that get databound. In general, it's a better way to design your code anyway.Monday, June 13, 2005 11:39 PM -
User-858571521 posted
Hi,
I am new to the forum and have a similar problem discussed in this thread. I have a gridview with paging enabled. The rows has a template field containing a textbox. On paging, i.e., gv_PageIndexChanging, I need to extract the value in that textbox and save its value before the new page is rendered. Here is my code:
Protected Sub gvProductList_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvProductList.PageIndexChanging
gvProductList.PageIndex = e.NewPageIndex
Response.Write("PageIndexChanging - " & gvProductList.Rows.Item(0).Cells(0).Text & "<br>")
End Sub
The problem I am having is the value extracted from the cell is alway lagging one page behind. I.e., if page 1 cell contains "1" and page 2 cell contains "2" and page 3 cell contains "3", then as I click through the pages 1, 2, 3, the above code will output "1", "1", "2", instead of "1", "2", "3".
My final goal is to save the data for all rows on current page to a collection before moving to th new page. Any help would be greatly appreciated.
Thanks.Thursday, June 23, 2005 2:35 AM -
User-1119785146 posted
I took this approach for non-sensitive data. I made both the column width and font-size 0px. You might also need to set relevant border widths equal to 0, change the font color to the same as the background, and set padding to 0px.
Monday, June 27, 2005 4:41 AM -
User-1641236696 posted
Hi I have a situation where I need to access the datakeys. I have created a gridview and setup 2 datakeynames "ID, Active" I need to retrieve the value of active inside each row in the datagrid. Depending on the value of active I will set the row color to red. Here is what I have so far....which isn't much,
When I try anything with the DataKeys I get this - Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){
GridView gv = (GridView)sender;
// code
e.Row.BackColor = System.Drawing.Color.Red;
}
I would appreciate any help you can offer, many thanks
PaulThursday, June 30, 2005 1:59 PM -
User1944777292 posted
Redtap, it's hard to tell why you're getting an IndexOutOfRange exception without seeing your code. The index should be the index of the row in the current page. If you have turned off viewstate, you won't get DataKey values until you call DataBind.
You can post your code for more help.Thursday, June 30, 2005 3:51 PM -
User-1641236696 posted
I dont have any code, that is the start of something i am trying to achieve. I have a gridview on the design surface which gets its data from an objectdatasource. I call a rowdatabound event on the gridview to look at each row as its being bound. I want to find out if 'Active' = true or false. If it is false I will set that row colour to red. I start this off by havin a column which held all the values for each row to say wether that row was active or not. once id coloured the rows red or not i then set the column to invisible on prerender. I am interested in doing a similar thing with datakeys and datakeynames not sure where to start
many thanks
PaulThursday, June 30, 2005 4:07 PM -
User1944777292 posted
It sounds like you're on the right track. Set DataKeyNames="ID, Active" and then on RowDataBound you can look at the value of your DataKeys. It would look like this:
private void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
if (GridView1.DataKeys[e.Row.RowIndex]["Active"] == false) {
e.Row.BackColor = Color.Red;
// or you can just set visible now...
e.Row.Visible = false;
}
}
}
Hope this helps...Thursday, June 30, 2005 5:21 PM -
User-1641236696 posted
this is the code I had to use in the end to avoid errors, it is adapted from what you showed me, thank you very much for your help, as I am learning this stuff at the moment, I would liek to refrence 'active' as a boolean but for some reason it says i cannot use == operator on bool in the error message, so its a string now. Thanks again
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e){
if (e.Row.RowType == DataControlRowType.DataRow){
//Response.Write(GridView1.DataKeys[e.Row.RowIndex]["Active"]); if ((String)GridView1.DataKeys[e.Row.RowIndex]["Active"] == "0"){
e.Row.BackColor = System.Drawing.
Color.FromArgb(80, 90, 190); // or you can just set visible now... // e.Row.Visible = false;}
}
Thursday, June 30, 2005 7:04 PM -
User96271123 posted
Hello, i need an example of use of DataKeys by to edit and delete in a gridview, an example is better that 1000 words. Thanks,
ENIAC boyWednesday, August 17, 2005 6:20 PM -
User300685930 posted
I wrote an article and have the complete project file on my blog. The article is at this URL:
http://peterkellner.net/archives/2005/07/18/18/
Wednesday, August 17, 2005 6:33 PM -
Wednesday, August 17, 2005 6:33 PM
-
User2082534536 posted
it's good to know about datakeynames for aspnet 2.0. anyway i have been using datakeyfield for 1.1 for datasource with single column PK. What is the ideal way of storing clustered/complex keys in 1.1?Wednesday, October 19, 2005 11:33 PM -
User-1946611130 posted
I had this issue when trying to use the id on databound and tie in with image buttons in grid to open reports or more detailed info pages. Despite no responses to my posts I realised that the paging and datakey was the issue, here is my workround to get at id:
Public
Sub QuoteItem(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvQuote.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then
If TypeOf (e.Row.Cells(0).Controls(0)) Is ImageButton Then
'get index for item on page n
Dim index As Integer = Convert.ToInt32(e.Row.DataItemIndex)
'this fails when going to page 2+ - Dim item As String = gvQuote.DataKeys(index).Value.ToString
'this function solved issue
Dim item As String = getIndex(gvQuote, index)
Dim theURl As String = UrlBase & "/reports/reports.aspx?report=quoteshort.rpt&id=" & item
Dim btnSummary As ImageButton = CType(e.Row.Cells(3).Controls(0), ImageButton)
btnSummary.Attributes("onclick") = "javascript:openwin('report','" & theURl & "',850,650,'no','no','yes');"
End If
End If End Sub
Function getIndex(ByVal grid As GridView, ByVal item As Integer) As String
Dim Pindex As Integer = grid.PageIndex * grid.PageSize If item < grid.PageSize Then
Return grid.DataKeys(item).Value.ToString
else
Return grid.DataKeys(CInt(item / Pindex) - 1).Value.ToString
End If End FunctionThursday, November 24, 2005 5:24 AM -
User791064739 posted
It sounds like you're on the right track. Set DataKeyNames="ID, Active" and then on RowDataBound you can look at the value of your DataKeys. It would look like this:
private void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
if (GridView1.DataKeys[e.Row.RowIndex]["Active"] == false) {
e.Row.BackColor = Color.Red;
// or you can just set visible now...
e.Row.Visible = false;
}
}
}
Hope this helps...
That solved the problem for me. Thanks!
Sunday, November 27, 2005 11:54 AM -
User1949343134 posted
Dont know if anyone is following this thread anymore but there is one more question that has not been answered in this thread...hehe.
If you have an ObjectDataSource with and update method, and a GridView with a hidden ID column, what then? I am not able to figure it out... I dont want to add code in the EventHandler!
Example code:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="DataLayer.DataObj" SelectMethod="GetTestDT" UpdateMethod="UpdateTest">
<UpdateParameters>
<asp:Parameter Name="ID" Type="String" />
<asp:Parameter Name="Name" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" DataKeyNames="ID" DataSourceID="ObjectDataSource1" runat="server" AutoGenerateColumns="False" OnRowUpdated="GridView1_RowUpdated">
<Columns>
<asp:TemplateField HeaderText="ID" Visible="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
Thursday, December 29, 2005 3:55 AM -
User300685930 posted
this thread is kind of long, but basically it says anything you put in the datakeynames will be stored in the viewstate (even if it is invisible). that is, both id_file and filedescr will be treated as if they are visible in regards to getting data from the viewstate.
DataKeyNames="id_file,filedescr"
Thursday, December 29, 2005 8:53 AM -
User1949343134 posted
Programming is facinating, I must say!
Today I came to work and just hit the run button, and it worked great...
The code I posted above that is, no code in any events!
Friday, December 30, 2005 6:57 AM -
User1944777292 posted
Glad you got it working, Kal-El. As mentioned above, using DataKeyNames is the best solution.Friday, December 30, 2005 3:02 PM -
User-1666213938 posted
I am trying to do the same thing.
I have first three boundfields in gridview set invisible and then i would like to retrieve these values on row command but it returns null.
can you please send me something in vb.net and explain how does this work. I am really confuse on this one why it forces me to keep everything visible if i want to access value of it later on.
thanks,
Hiral
Monday, March 13, 2006 12:13 AM -
User453669705 posted
The bug was that we cleared out the data keys when not bound to a data source control when handling the paging event. You can work around the issue by handing the RowCommand event. If the CommandName is "Page", it's a paging event. The DataKeys property should still be populated in the RowCommand event. Or, you can wait until after you're done paging and call DataBind. In the OnDataBound event, you can retrieve the key values.So, I'm confused and dissapointed.... If you're not using a datasource control and have some paging the datakeynames is pretty much useless? Like the one guy posted, I have a tiered application and I'm binding to a datatable within a dataset. I need to be able to access two keys in my data, which I though the nice new datakeynames deal was supposed to be for. I can get the first value just fine, but the second value isn't even in the array. So it looks like this:
In the ascx, grid has DataKeyNames="DtlKey, GrpKey" GrpKey comes out fine
In the .cs, I set grid.DataSource = theDataset.Table; and call grid.DataBind()
In a _RowEditing event I use:
Guid
tGrpID = new Guid(grd.DataKeys[e.NewEditIndex].Values["GrpKey"].ToString()); Guid tDtlID = new Guid(grd.DataKeys[e.NewEditIndex].Values["DtlKey"].ToString());GrpKey comes out fine, but DtlKey always blows. What the hell? [:@]
Sunday, March 19, 2006 4:22 PM -
User453669705 posted
Ok, you can ignore my previous post [:$]. If you call grid.DataBind() after paging the key values are there. I wasn't seeing both keys in my previous example because I added the additional key name while debugging and the browser had stuff cached in control state, yadda yadda yadda, so I reset IIS, Rebuilt my project and voila, there everything is. This was, of course after I realized I really don't need both keys anyway. Duh.!
Sunday, March 19, 2006 4:55 PM -
User1015812913 posted
Ade's posted VB response worked a treat. Assuming you've got the viewstate switched on already.
Nothing like a coded example to make things clear.Wednesday, April 12, 2006 12:05 PM -
User2105014182 posted
What if my datasource is just an array? How do I use datakeynames and datakey?
Tuesday, July 4, 2006 9:13 AM -
User-1603166410 posted
this should help- the easy way to get datakeys
if you're not using them, try creating some (even if just from the index of a basic array):
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Int32.Parse(e.CommandArgument.ToString());
int pk = Int32.Parse(GridView1.DataKeys[ index ].Value.ToString());
}<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>Monday, July 10, 2006 7:01 PM -
User-1386562875 posted
Hi all,
If use the following code to loop through certain rows of the GridView
For Each gvr As GridViewRow In GridView1.Rows 'Programmatically access the CheckBox from the TemplateField Dim cb As CheckBox = CType(gvr.FindControl("RowLevelCheckBox"), CheckBox) 'If it's checked, show control If cb.Checked Then currentRowIndex = gvr.RowIndex.ToString() ShowControls(TODO, gvr.Cells(0).Text, gvr.Cells(1).Text, CType(gvr.Cells(2).Text, Integer), CType(gvr.Cells(3).Text, Integer)) End If Next
In the TODO spot I want the value of the DataKeyNames="CONTROL_ID"How do I do this?
Friday, December 15, 2006 5:08 AM -
User1777427112 posted
I found answers in this thread very helpful. Thanks all :)Tuesday, April 24, 2007 4:40 PM -
User1606166975 posted
gridView.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString()
Friday, August 31, 2007 6:36 AM -
User2094048690 posted
hello
I had the same problem that you have and I found this solution . It might not be the best but it works :)
1.make all fields visible in GridView
than in the runtime on event gridview_RowDataBound set the field that you want to visible =false;
Monday, February 4, 2008 12:19 PM -
User652355303 posted
Hi,
Here's the link to resolve this problem..
http://www.thescripts.com/forum/thread471276.html
try also this one..
Monday, February 25, 2008 10:36 PM -
User-1211738364 posted
I have the following gridview, same request: I need the value of the hyperlinked text or the value of the hidden column (Filename). When I use DataKeys I only get the FileID. Thank you
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="FileID,FileName" DataSourceID="FileListDataSource" AllowPaging="True" PageSize="6">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="FileName"
DataNavigateUrlFormatString="{0}" DataTextField="FileName"
HeaderText="View File" />
<asp:BoundField DataField="FileName" HeaderText="FileName"
SortExpression="FileName" Visible="False" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkDeleteFile" runat="server" CausesValidation="False"
CommandName="Delete" onclick="lnkDeleteFile_Click"
onclientclick="JavaScript:return confirm('Are you sure?')" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Friday, March 21, 2008 8:44 PM -
User-1211738364 posted
I have the following gridview, same request: I need the value of the hyperlinked text or the value of the hidden column (Filename). When I use DataKeys I only get the FileID. Thank you
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="FileID,FileName" DataSourceID="FileListDataSource" AllowPaging="True" PageSize="6">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="FileName"
DataNavigateUrlFormatString="{0}" DataTextField="FileName"
HeaderText="View File" />
<asp:BoundField DataField="FileName" HeaderText="FileName"
SortExpression="FileName" Visible="False" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkDeleteFile" runat="server" CausesValidation="False"
CommandName="Delete" onclick="lnkDeleteFile_Click"
onclientclick="JavaScript:return confirm('Are you sure?')" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Friday, March 21, 2008 8:44 PM -
User638938195 posted
Hi,
i want to hide my table column at the server side. But I am unable to hide it. I am using style="table-layout:fixed;" for the table and on the pageload i am setting the visible=false for the first row of the table and the column which I want to hide.
Thanks
Wednesday, October 15, 2008 10:01 AM -
User-265690979 posted
I think you guys take it for granted how to use Datakeys , But i didn't know and i had to find it .
So i though that would be useful for others like me that don't know how to use Datakeys specially when we have more than one Datakeys
//In GridView RowCommand Evens: index = Convert.ToInt32(e.CommandArgument); row = gvCommonData.Rows[index]; //If we have more than one DataKeys ( ItemId is the field that is visbile = false and myGridView.DatakeyName = Itemid) int ItemId = Convert.ToInt32(gvCommonData.DataKeys[index].Values["ItemId"]); //else int ItemId = Convert.ToInt32(gvCommonData.DataKeys[index].Value);
Wednesday, July 15, 2009 5:50 AM -
User-541930774 posted
//In GridView RowCommand Evens: int index = Convert.ToInt32(e.CommandArgument); GridViewRow row = GridView1.Rows[index]; //If we have more than one DataKeys ( ItemId is the field that is visbile = false and myGridView.DatakeyName = Itemid) int ItemId = Convert.ToInt32(GridView1.DataKeys[index].Values["ItemId"]); //else int ItemId = Convert.ToInt32(GridView1.DataKeys[index].Value);
Friday, March 5, 2010 6:04 AM -
User-1116135949 posted
Hi,
I found some new workaround.
1. Make all the columns visible true in design mode.
2. In code behind, bind the data to the grid, and immediately change the visibility of the columns (which should be hidden) to visible = false.
This works for me. I am open to new ideas please..
Sahana
Tuesday, March 16, 2010 8:19 AM -
User-510141866 posted
First thanks for this long conversation.
I also have the same type of problem with CheckBox.
In my gridview has Checkbox in 30th Column, and I make Visible false in certain condition(but in background it is in True value). Now when button in postback the checkbox.Checked return false value even its in true(Column in false )
After read this post , I changed Column.visible=false set as in RowDataBound event as e.Row.Cells[30].visible=false (Both Header and DataRow) it solved my problem which means it return as its value.
Thanks.....
Friday, February 21, 2014 9:05 AM