2008-12-11 36 views
0

我试图从gridview,itryed克隆()尝试获得完整的数据副本,尝试从DataSouce转换DataView,但总是得到空值或不能获取数据,请存在一种方法来从gridview复制数据,修改它,然后重新加载它?或者直接在gridview中修改一些行? 在此先感谢!如何从gridview获取副本?

回答

1

您可以尝试使用OnRowDataBound属性做这样的事情

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.Header) 
    { 
     //HeaderStuff 
    } 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     ObjectTye objectType = (ObjectType)e.Row.DataItem; 
     // and doing some stuff with the properties 
     e.Row.Cells[0].Text = objectType.SomeProperty.ToString(); 
     LinkButton deleteLnk = (LinkButton)e.Row.FindControl("lnkDelete"); 
     deleteLnk.Attributes.Add("onclick", "javascript:return " + 
      "confirm('Are you sure you want to delete this')"); 
     deleteLnk.CommandArgument = e.Row.RowIndex.ToString(); 
    } 
} 
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    int rowIndex = int.Parse(e.CommandArgument.ToString()); 
    GridViewRow row = GridView1.Rows[rowIndex]; 

    ObjectType objectType = new ObjectType(); 
    objectType.StringProperty = row.Cells[0].Text; 
} 
+0

即时通讯使用RowCommand – 2008-12-11 18:26:55

+0

可以使用与TemplateFields一起工作?我得到一个按钮和复选框 – 2008-12-11 18:29:53

0

究竟是你想要处理的数据?它也是一个数据网格或数据视图,并在什么框架?如果它是您试图从中复制行的数据网格,请循环访问数据网格行并将行值添加到数组列表中。

0

为什么不绑定的第二格第一网格绑定同一消息来源?

DataGridView1.DataSource = yourList; 
DataGridView1.DataBind(); 
... 
DataGridView2.DataSource = yourList; //or = DataGridView1.DataSource; 
DataGridView2.DataBind(); 

只要数据没有改变,你应该得到一个确切的副本。