Click here to Skip to main content
15,892,537 members

GridView changes not showing in binded Datatable

Member 3616119 asked:

Open original thread
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 ..
Tags: C# (C# 2.0), .NET (.NET 2.0), ASP.NET, Gridview, DataTable, Data binding

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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