ASP.net Gridview help

coder_t2

Member
Nov 6, 2009
92
0
0
Hey guys, I need some help here for my Gridview in ASP.net. I am trying to make it editable, where someone can change a value and it will update in a database. However, I can't seem to pull the edited text. I keep getting the original text. I already searched with google and it did exactly what other people did, but it's still not giving me the edited text. Code is below for Gridview.


<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server" OnRowDataBound="ItemDataBoundEventHandler1" bgcolor='#FFFBD6' onrowediting="GridView1_RowEditing"
onrowcancelingedit="GridView1_RowCancelingEdit" onrowupdating="GridView1_RowUpdating" >
<Columns>
<asp:commandfield showeditbutton="True" />
<asp:TemplateField HeaderText="CUSTCOL_16" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"CUSTCOL_16") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt" runat="server" Width="100px" Text='<%#DataBinder.Eval(Container.DataItem,"CUSTCOL_16") %>'></asp:TextBox>
</EditItemTemplate>

</asp:TemplateField>

</Columns>
</asp:GridView>
</div>
</form>
</body>


This is the script code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView2.aspx.cs" Inherits="WebApplication1.GridView2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>

<script runat="server">

//DataSet ds = new DataSet();

private void Page_Load(Object sender, EventArgs e)
{
Populate();
}

private void Populate()
{
SqlConnection dbconn = new SqlConnection();
dbconn.ConnectionString = "Data Source=comp1;Initial Catalog=DB1;" +
"Integrated Security=SSPI";
dbconn.Open();
SqlCommand dbcomm = new SqlCommand();
dbcomm.Connection = dbconn;
dbcomm.CommandTimeout = 0;
dbcomm.CommandType = CommandType.Text;
dbcomm.CommandText = "select * from dbo.adhoc";
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = dbcomm;
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}

public void ItemDataBoundEventHandler1(object
sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover",
"this.style.backgroundColor='Silver'");
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor='#FFFBD6'");
}
}

protected void GridView1_RowEditing(Object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex; //set to selected row
Populate();

}

protected void GridView1_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1; //set to no selection
Populate();

}
protected void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
TextBox t = new TextBox(); ;
GridViewRow row = GridView1.Rows[e.RowIndex];
if (row != null)
{

t = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("txt");
if (t != null)
{
Response.Write("The Text Entered is" + t.Text);
//Trying to get this Response to have updated text, insted of original text. This is just for //testing purposes. After I know I am pulling the updated Text I will make it update //Database.
}
else
Response.Write("T IS NULL");
}

GridView1.DataBind();
}
 

Oyster

Member
Nov 20, 2008
151
0
0
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"CUSTCOL_16") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt" runat="server" Width="100px" Text='<%#DataBinder.Eval(Container.DataItem,"CUSTCOL_16") %>'></asp:TextBox>
</EditItemTemplate>
}

Quickly looking at this, your DataBinding operations should be binding to the data, not evaluating it. That is, replace DataBinder.Eval with DataBinder.Bind. Eval is a one-way data binding call representing reading data, not writing. Comparatively, Bind is a two-way call, allowing you to update data, too.

Also, in your RowUpdating event, you're not resetting the EditItemIndex (that is, set it to -1, just like you do in the Canceling event, after you've updated the data).

As a side note, I am assuming this is code for testing purposes only? Because you're making a roundtrip to the database on every page load...
 

coder_t2

Member
Nov 6, 2009
92
0
0
Ok. I made those changes. But the thing to fix the problem was the ispostback property. I change page_load to
if(!ispostback)
{
Populate();
}

Now I need to figure out why I am getting double of column CustCol_16. I think the issues is Gridview is populating from my query, and then itemtemplate is adding another column for Custcol_16. How do I hide the columns from the query?
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
If you're going to update data on code behind, DataBinder.Eval is fine.

The reason you are getting old data is because of the following statement.

Code:
private void Page_Load(Object sender, EventArgs e)
{
Populate();
}

You are populating the gridview every postback so when you are hitting the "update" button, it will rebind with the OLD data before your updating event captures the new text.

Change that to

Code:
private void Page_Load(Object sender, EventArgs e)
{
If Not IsPostBack Then
     Populate();
}

Edit: Looks like you figured out the old text values. As to your new issues, what do you mean you are doubling columns? Do you mean you are getting doubling of the same rows?
 
Last edited:

Snapster

Diamond Member
Oct 14, 2001
3,916
0
0
If you're going to update data on code behind, DataBinder.Eval is fine.

The reason you are getting old data is because of the following statement.

Code:
private void Page_Load(Object sender, EventArgs e)
{
Populate();
}

You are populating the gridview every postback so when you are hitting the "update" button, it will rebind with the OLD data before your updating event captures the new text.

Change that to

Code:
private void Page_Load(Object sender, EventArgs e)
{
If Not IsPostBack Then Then
     Populate();
}

Ouch, vb mixed with c#, even if that's not valid it's nasty :D

Ok. I made those changes. But the thing to fix the problem was the ispostback property. I change page_load to
if(!ispostback)
{
Populate();
}

Glad that one was worked out :)

Now I need to figure out why I am getting double of column CustCol_16. I think the issues is Gridview is populating from my query, and then itemtemplate is adding another column for Custcol_16. How do I hide the columns from the query?

If you are specifying your own columns, set the datagrid's autogeneratecolumns property either in the .aspx or the codebeind to false.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
LOL, I had a brainfart and for some reason thought the code was VB. Hahaha, that's a first.