Updating Db from GRIDVIEW with Hidden Column [modified]
-
Please your help, I using Vs.Net 2005 and the ASP.Net 2.0. My problem be with the GRIDview and the SQLDataSource. My Dataview work as planned and can update / delete perfect when all columns are visible in the GRIDView. However big problem is made when i make one of columns to Invisible. This column data is not found and so database update not work and cause error. Anyone can help me please. Most greatfull marconi! -- modified at 10:51 Wednesday 22nd November, 2006
-
Please your help, I using Vs.Net 2005 and the ASP.Net 2.0. My problem be with the GRIDview and the SQLDataSource. My Dataview work as planned and can update / delete perfect when all columns are visible in the GRIDView. However big problem is made when i make one of columns to Invisible. This column data is not found and so database update not work and cause error. Anyone can help me please. Most greatfull marconi! -- modified at 10:51 Wednesday 22nd November, 2006
I don't know if that's yoyr problem, as you didn't write any code here, but it could be because data of hidden columns are not stored in viewstate, so they are not available after postback. Small trick is to make column invisible after you databind data: myGridViewColumn.Visible = true; myGridView.DataBind(); myGridViewColumn.Visible = false; Try it, maybe that solves your problem. Pilo
-
I don't know if that's yoyr problem, as you didn't write any code here, but it could be because data of hidden columns are not stored in viewstate, so they are not available after postback. Small trick is to make column invisible after you databind data: myGridViewColumn.Visible = true; myGridView.DataBind(); myGridViewColumn.Visible = false; Try it, maybe that solves your problem. Pilo
Hello, I am using the wizard to make the SqlDataSource and Gridview. When i make update all is working fine when all columns are visible. But when i hide 1 or more of the column, i get error because it not find the data in this column. So cannot add this data to database. Any help how to do? -- modified at 10:52 Wednesday 22nd November, 2006
-
I don't know if that's yoyr problem, as you didn't write any code here, but it could be because data of hidden columns are not stored in viewstate, so they are not available after postback. Small trick is to make column invisible after you databind data: myGridViewColumn.Visible = true; myGridView.DataBind(); myGridViewColumn.Visible = false; Try it, maybe that solves your problem. Pilo
This problem is with GRIDVIEW not DataView sorry!
-
Hello, I am using the wizard to make the SqlDataSource and Gridview. When i make update all is working fine when all columns are visible. But when i hide 1 or more of the column, i get error because it not find the data in this column. So cannot add this data to database. Any help how to do? -- modified at 10:52 Wednesday 22nd November, 2006
I alredy wrote you possible reason of your problem... Anyway, why do you want to store data to GirdView, if they are not displayed? If they are IDs, then use GridView.DataKeys to store IDs. Search for it on MSDN if you want to know how to use it properly. Pilo
-
I alredy wrote you possible reason of your problem... Anyway, why do you want to store data to GirdView, if they are not displayed? If they are IDs, then use GridView.DataKeys to store IDs. Search for it on MSDN if you want to know how to use it properly. Pilo
Ok, maybe you not understand problem i try to describe. I display the data / informations in GridView via SqlDataSource. But one column must be hidden so not show this data. But if column is hidden then cannot update database because the hidden field column is not added to update statement, and my database say this field must not be empty, so DB wont accept update. Problem is not because of DataKey but because data is missing when try to add to database. So how to make data collected from hidden column to SqlDataSource Update statement? Thank, helps is appreciated much.
-
Ok, maybe you not understand problem i try to describe. I display the data / informations in GridView via SqlDataSource. But one column must be hidden so not show this data. But if column is hidden then cannot update database because the hidden field column is not added to update statement, and my database say this field must not be empty, so DB wont accept update. Problem is not because of DataKey but because data is missing when try to add to database. So how to make data collected from hidden column to SqlDataSource Update statement? Thank, helps is appreciated much.
I understand your problem. You probably don't understand what I'm trying to tell you :) This hidden column WILL be empty, unless you fill it again after postback, or make it visible BEFORE DataBind() is called. Data stored in hidden column are not stored in ViewState, so after postback, this column will be empy - application has no way how to refresh this data automaticaly. But if it is hidden, it means data are not going to be changed anyway so why do you want to update them back into database??? Exclude this column from your data source and you solve your problem. One reason I see why you could want to have this column is to store ID of row into it. But for storing IDs in .NET 2.0 you should use DataKeys property of GridView instead of hidden column. I think some 'smart' head in Microsoft decided that hidden columns should not contain any data (security and performance reason probably). Thats why they introduced DataKeys property. Try to answer to following question and then I could be able to help you better: why do you need to have this column in your GridView? If you dont need it, just change your select and update statements and exclude it. If you need it, than you have to think up a workaround, similar as I posted you in my first post. Pilo
-
I understand your problem. You probably don't understand what I'm trying to tell you :) This hidden column WILL be empty, unless you fill it again after postback, or make it visible BEFORE DataBind() is called. Data stored in hidden column are not stored in ViewState, so after postback, this column will be empy - application has no way how to refresh this data automaticaly. But if it is hidden, it means data are not going to be changed anyway so why do you want to update them back into database??? Exclude this column from your data source and you solve your problem. One reason I see why you could want to have this column is to store ID of row into it. But for storing IDs in .NET 2.0 you should use DataKeys property of GridView instead of hidden column. I think some 'smart' head in Microsoft decided that hidden columns should not contain any data (security and performance reason probably). Thats why they introduced DataKeys property. Try to answer to following question and then I could be able to help you better: why do you need to have this column in your GridView? If you dont need it, just change your select and update statements and exclude it. If you need it, than you have to think up a workaround, similar as I posted you in my first post. Pilo
Thanks you, I am greatful for your answer. Your idea to remove is very good. I will do this. Is simple but overlooked way! ;-)) But also was trying to learn proper way not just a workaround. I have one question finally. If use wizards to create DataView how do access / call columns of dataview to make invisible / hidden?