asp.net网格视图批量更新所有细胞 [英] asp.net grid view bulk updating all cells

查看:132
本文介绍了asp.net网格视图批量更新所有细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的是asp.net网格视图从table.I我能够成功加载数据我的SQL数据加载数据。

I am using a asp.net grid view to load data from my sql data table.I am able to successfully load the data.

SQL表DESGIN:
3 cloumns:位置,名字,姓氏

Sql Table Desgin: 3 cloumns:Location,Firstname,LastName

位置是主键。

设计:

Aspxpage具有在底部的两个按钮的GridView:

Aspxpage has a gridview which has two buttons in the bottom:


  1. 编辑

  2. 保存

当用户点击编辑按钮在GridView的所有单元均采用可编辑的,这样用户可以编辑和保存的值。

When user hits Edit button all the cells in the gridview are made editable so that user can edit and save the values.

我的问题与保存按钮在哪里我无法编辑的数据回存到SQL就出现了。

My problem arises with the save button where i am unable to save the edited data back to the SQL.

下面是code的保存按钮点击:

Here is the code for the save button click:

protected void btnSave_Click(object sender, EventArgs e)
{
    int RowIndex=0;

    GridViewRow row = (GridViewRow)gvres.Rows[RowIndex];

    TextBox txtLanguage1 = row.FindControl("txtFName") as TextBox;
    TextBox txtLanguage2 = row.FindControl("txtLName") as TextBox;

    SqlConnection myConnection = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("UPDATE UsersTable SET FirstName = @FirstName, LastName = @LastName WHERE Location = @Location", myConnection);


    cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text.Trim());
    cmd.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim());

    myConnection.Open();
    cmd.ExecuteNonQuery();
    gvusers.EditIndex = -1;
    DataBind();
}

例外:必须声明标量变量@location

Exception:"Must declare the scalar variable "@Location"."

推荐答案

您需要一个参数添加到名为@locationSqlCommand对象。你提到的位置是在网格中的一列---你可以读取该列中的值(类似于你是如何得到的姓和名的值),也可以指定位置作为关键数据和从网格的DataKeys属性得到它。

You need to add a parameter to the SqlCommand object named "@Location". You mention that Location is one of the columns in your grid---you can either read the value from the column (similar to how you are getting the first and last name values), or you can specify "Location" as the data key, and get it from the DataKeys property of the grid.

我看 ASP.NET现实世界的控制项目上codePLEX 。它允许批量编辑,它足够聪明,只更新已更改的行。

I'd look at the ASP.NET Real World Controls project on Codeplex. It allows for bulk editing and it smart enough to only update the rows that have changed.

这篇关于asp.net网格视图批量更新所有细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆