Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a GridView setup which I've tried to bind to DataTable .. I then make some changes to the GridView's values through the webpage .. Then I go behind the code of a button click, and see that the DataTable still has the old values in it, even though visually the new values are shown on the webpage (because the user typed them in) ...

ASPX Markup code for the GridView:

ASP.NET
<asp:gridview ID="ESBAndTSRValuesInputGridView" runat="server" ShowFooter="true" AutoGenerateColumns="false">
	<Columns>
	<asp:BoundField DataField="AwardID" HeaderText="Award ID" Visible="false" />
	<asp:BoundField DataField="AwardName" HeaderText="Award Name" />
	<asp:TemplateField HeaderText="ESBValue">
		<ItemTemplate>
			<asp:TextBox ID="TextBox1" Text='<%# Eval("ESBValue") %>' runat="server"></asp:TextBox>
		</ItemTemplate>
	</asp:TemplateField>
	<asp:TemplateField HeaderText="TSRValue">
		<ItemTemplate>
			<asp:TextBox ID="TextBox2" Text='<%# Eval("TSRValue") %>'  runat="server"></asp:TextBox>
		</ItemTemplate>
	</asp:TemplateField>
	</Columns>
</asp:gridview>


Initializing GridView data from code-behind of a button click:

C#
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("AwardID", typeof(string)));
dt.Columns.Add(new DataColumn("AwardName", typeof(string)));
dt.Columns.Add(new DataColumn("ESBValue", typeof(string)));
dt.Columns.Add(new DataColumn("TSRValue", typeof(string)));
DataRow[] PSPAwards = dtAwards.Select("AWARDTYPE = 'PSP'");
foreach (DataRow dr in PSPAwards)
{
	dt.Rows.Add(dr["AWARDID"].ToString(), dr["AWARDNAME"].ToString(), "0", "100");
}
ViewState["ESBAndTSRValuesDataTable"] = dt;
ESBAndTSRValuesInputGridView.DataSource = dt;
ESBAndTSRValuesInputGridView.DataBind();


Trying to get new data on a button click:

C#
DataTable dt = (DataTable)ViewState["ESBAndTSRValuesDataTable"]; //Fetching the DataTable from ViewState.
dt.AcceptChanges(); //Even after this, data in dt still shows the original default values, and not the edited values user had input.

// Tried these approaches too:

dt = ESBAndTSRValuesInputGridView.DataSource as DataTable; // When I execute this, dt becomes null.
DataSet ds = ESBAndTSRValuesInputGridView.DataSource as DataSet; // When I execute this, dt becomes null again.


In the above code, you can see that I initialize the rows with default values 0 and 100 .. This is what I see during a debug event after making changes to these values from the webpage ..

How can I make the GridView automatically persist all changes done to it to its linked DataTable ?

I have also tried using Session instead of ViewState, but it didn't help ..

I'm using .NET 2.0 Framework with VS2005 ..
Posted
Updated 30-Jun-13 22:16pm
v2
Comments
Thanks7872 1-Jul-13 4:48am    
What you want to do? I mean what you are aimed at after this code?
Member 3616119 1-Jul-13 4:53am    
I want to read the updated values and process them, row by row .. There is an internal function in which I pass the EPS and TSR values .. Problem is that suppose user changes the default EPS and TSR values, then I need the updated values in the event after the button click ..
Thanks7872 1-Jul-13 4:57am    
I am not getting what you say but,gridview is used just to display data that comes from somewhere.So if you want to change data in gridview then it should also be changed to the place from where you binded before. If you binded gridview with database(using datatable),then if you want to change values in gridview then you have to make changes to database,because you can not bind datable from gridview.Not possible. If you want more,let me know.
Member 3616119 1-Jul-13 5:18am    
Well, in my case no Database is involved. I just want to bind the GridView to a DataTable, and then populate that DataTable with 4 columns and some rows, with some default data. Once this is done, I want to display the GridView with the default data in the binded DataTable, and give the user the option to make changes to that data. If the user makes any changes, then I want to changes to reflect in the binded DataTable, and this is exactly what I can't seem to get done ... I don't think there is a requirement that data has to come from a database .. Also, the whole purpose of the GridView is to display data and assist in changing it. If the user has to go to the source to change the data, then it essentially defeats the purpose of having a GridView in the first place ..
Thanks7872 1-Jul-13 5:24am    
If the user has to go to the source to change the data, then it essentially defeats the purpose of having a GridView in the first place ..? Whats this? Do you know anything about gridview editing?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900