473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ASP.NET 2 GridView.

I got to playing with ASP.NET 2.0. So I have the following code:
CODE
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="QuestionID"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="Description"
HeaderText="??????"></asp:BoundField>
<asp:CheckBoxField DataField="IsActive" HeaderText="???." />
<asp:CommandField ButtonType="Image"
CancelImageUrl="img/button_cancel.png" EditImageUrl="img/edit.png"
ShowEditButton="True" UpdateImageUrl="img/button_ok.png" />
</Columns>
</asp:GridView>
In .cs file:
CODE
SqlDataSource ds;

protected void BindData()
{
ds = new
SqlDataSource(WebConfigurationManager.ConnectionSt rings["AnketaDBConnectionString"].ToString(),
"SELECT QuestionID,Description,IsActive FROm Question");
GridView1.DataSource = ds;
GridView1.DataBind();

}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}

protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
Response.Write(e.Keys.Count.ToString());
Response.Write(e.NewValues.Count.ToString());
}

protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}

protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}

You see that it outputs number of keys and new values on update.
Problem is, both numbers are zero when I press edit, preform editing
and press ok, I cannot update anything. What's wrong with that?

Nov 19 '05 #1
1 3461
If you want to the grid to do automatic updates back to the database you
need to use the new declarative databinding model. SO, IOW, you need a <asp:SqlDataSource>
and to set the DataSourceID on your GridView.

http://beta.asp.net/QUICKSTART/aspne...a/default.aspx

-Brock
DevelopMentor
http://staff.develop.com/ballen
I got to playing with ASP.NET 2.0. So I have the following code:
CODE
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="QuestionID"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="Description"
HeaderText="??????"></asp:BoundField>
<asp:CheckBoxField DataField="IsActive" HeaderText="???." />
<asp:CommandField ButtonType="Image"
CancelImageUrl="img/button_cancel.png" EditImageUrl="img/edit.png"
ShowEditButton="True" UpdateImageUrl="img/button_ok.png" />
</Columns>
</asp:GridView>
In .cs file:
CODE
SqlDataSource ds;
protected void BindData()
{
ds = new
SqlDataSource(WebConfigurationManager.ConnectionSt rings["AnketaDBConne
ctionString"].ToString(),
"SELECT QuestionID,Description,IsActive FROm Question");
GridView1.DataSource = ds;
GridView1.DataBind();
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
Response.Write(e.Keys.Count.ToString());
Response.Write(e.NewValues.Count.ToString());
}
protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
You see that it outputs number of keys and new values on update.
Problem is, both numbers are zero when I press edit, preform editing
and press ok, I cannot update anything. What's wrong with that?


Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the...
6
by: Nalaka | last post by:
Hi, I have a gridView (grid1), which as a templateColumn. In the template column, I have put in a gridView (grid2) and a ObjectDataSource (objectDataSource2). Question is... How to I pass the...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
5
by: Dick | last post by:
I have a GridView bound to an ObjectDataSource. I have a Button that calls GridView.DataBind. I want the row that is selected before the DataBind to still be selected afterwards. This happens...
1
by: joechipubik | last post by:
I have a GridView in a FormView that has as its datasource a DataTable that is stored in the session cache. When I first load the page the GridView is displayed correctly, but on subsequent loads...
3
by: Jeff | last post by:
Hey asp.net 2.0 In the source I posted below, there is a GridView (look at the bottom of the script): <asp:GridView ID="gvwOnline" runat="server"> </asp:GridView> I'm trying to assign a...
2
by: antonyliu2002 | last post by:
I've been googling for some time, and could not find the solution to this problem. I am testing the paging feature of gridview. I have a very simple web form on which the user can select a few...
5
by: Andrew Robinson | last post by:
I am attempting to better automate a Pager Template within a GridView. I am succesfully skinning a Drop Down List withing my control (the DDL is added to my control). I correctly populate the item...
6
by: RobertTheProgrammer | last post by:
Hi folks, Here's a weird problem... I have a nested GridView setup (i.e. a GridView within a GridView), and within the nested GridView I have a DropDownList item which has the...
3
by: Peter | last post by:
I have a GridView which is populated by List<ofObjects> Does anyone have example of how to sort the columns of this GridView? I have found examples without DataSourceControl but these use...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.