ASP.NET GridView Cell Editing替代方案

时间:2009-08-21 07:49:43

标签: asp.net data-binding

是否可以在不使用

的情况下编辑和更新GridView单元格

<asp:TemplateField>和/或

<%# Eval("Name") %>和/或

<%# Bind("Name") %>和/或

SqlDataSource?

注意:我只想在后台和SqlConnection,SqlCommand,ExecuteXXX等中使用C#代码。

注意:Plz为我提供代码(C#和aspx)或包含代码的网络链接。

3 个答案:

答案 0 :(得分:2)

在gridview标记中使用onroweditingonrowupdating ... 像这样的东西:

  <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
    GridLines="None" AllowPaging="true" AllowSorting="true" PageSize="5" DataKeyNames="Id"
    onpageindexchanging="GridView1_PageIndexChanging" 
    AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
    onsorting="GridView1_Sorting" onrowediting="GridView1_RowEditing">

我对winforms不太确定,但在网站上尝试这个..

 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    //your code that will edit/update.

}

通常,SqlcommandSqlconnection的代码类似于:

 SqlConnection con;
SqlCommand cmd;
DataSet ds;
SqlDataAdapter da;


 protected DataSet FillDataSet()
{
    string source = "Database=GridTest;Server=Localhost;Trusted_Connection=yes";
    con = new SqlConnection(source);
    cmd = new SqlCommand("proc_mygrid", con);
    ds = new DataSet();
    da = new SqlDataAdapter(cmd);
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();

    return ds;


}

希望这有帮助。

答案 1 :(得分:1)

我仍然不知道你为什么要与asp.net作斗争,但你可以使用 ObjectDataSource 做你想做的事。 ObjectDataSource 控件使用反射来调用业务对象的方法来选择,更新,插入和删除数据。您可以设置 ObjectDataSource 控件的 TypeName 属性,以指定要用作源对象的类的名称。

使用 ObjectDataSource 简历执行以下操作:

声明:

<asp:objectdatasource
  runat="server"
  id="ObjectDataSource1"
  typename="EmployeeLogic"
  selectmethod="GetAllEmployees"
  updatemethod="UpdateEmployeeInfo"
  dataobjecttypename="NorthwindEmployee" />

将方法创建为代码隐藏:

public List<NorthwindEmployee>GetAllEmployees()
{
 //your code here
}
public void UpdateEmployeeInfo(NorthwindEmployee emp) {
 //your code here
}

配置GridView并感到高兴:)

下面我提供了一些可能对您有帮助的链接:

http://www.manuelabadia.com/blog/PermaLink,guid,c72852ae-1fdd-4934-a715-f565ceaf21cc.aspx

http://msdn.microsoft.com/en-us/library/57hkzhy5.aspx

http://www.google.com.br/search?hl=en-us&q=objectdatasource+asp.net

答案 2 :(得分:0)

一种可能的解决方案可能是管理gridview的RowDataBound事件并设置像

这样的值
e.Row.Cells(i).Text = value