2012-11-27 40 views
0

我有一个从数据库中填充的gridview,我想允许用户编辑并添加到gridview。完成更改/添加后,将其提交到数据库。有人可以帮助我如何做到这一点?如何在写入数据库之前存储可编辑gridview的数据

这也是迄今为止

<asp:GridView ID="Grd" runat="server" AutoGenerateColumns="False" OnRowEditing="Grds_RowEditing" 
    OnRowCancelingEdit="Grd_RowCancelingEdit" OnRowUpdating="Grd_RowUpdating" Width="500px"> 
    <Columns> 
     <asp:TemplateField HeaderText="Name"> 
      <ItemTemplate> 
       <asp:HiddenField ID="NameID" runat="server" Value='<%# Bind("ID")%>' /> 
       <asp:Label ID="LblName" runat="server" Text='<%# Bind("Name")%>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Date"> 
      <EditItemTemplate> 
       <asp:TextBox ID="TxtDate" runat="server" Text='<%# Bind("Date") %>'></asp:TextBox> 
      </EditItemTemplate> 
      <ItemTemplate> 
       <asp:Label ID="LblDate" runat="server" Text='<%# Bind("Date")%>'></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:CommandField ShowEditButton="True" /> 
     <asp:CommandField ShowDeleteButton="True" /> 
    </Columns> 
</asp:GridView><asp:Button ID="btnok" Text="OK" Height="25" Width="25" runat="server" /> 

我的代码和代码背后

protected void Page_Load(object sender, EventArgs e) 
     { 
      BindGrid(int.Parse(hfID.Value)); 
     } 

     protected void BindGrid(int iPerson) 
     { 
      DataContext dc = new DataContext(ConfigurationManager.ConnectionStrings["DB"].ConnectionString); 

      var qry = from p in dc.GetDetails(iPerson) 
         select p; 

      Grd.DataSource = qry; 
      Grd.DataBind(); 
     } 
    protected void Grd_RowEditing(object sender, GridViewEditEventArgs e) 
     { 
      Grd.EditIndex = e.NewEditIndex; 
      //Bind data to the GridView control. 
      BindGrid(int.Parse(hfID.Value)); 

     } 
     protected void Grd_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
     { 
      Grd.EditIndex = -1; 
      BindGrid(int.Parse(hfID.Value)); 
     } 

     protected void Grd_RowUpdating(object sender, GridViewUpdateEventArgs e) 
     { 

     } 


protected void btOK_Click(object sender, EventArgs e) 
     { 
//update the database with changes 
     } 
+0

?或每行编辑 – sajanyamaha

+0

是的,该按钮不在gridview中。我刚刚更新了它的代码 – user1801525

回答

0

在你点击按钮试试这个,dtCurrentTable是你的GridView绑定的数据表。

int rowIndex = 0; 
StringCollection sc = new StringCollection(); 

for (int i = 1; i <= dtCurrentTable.Count; i++) 
{ 
//extract the TextBox values 
Label box1 = (Label)GridView2.Rows[rowIndex].Cells[0].FindControl("lblbedroom"); 
DropDownList box2 = (DropDownList)GridView2.Rows[rowIndex].Cells[0].FindControl("ddpAccomodates"); 
DropDownList box3 = (DropDownList)GridView2.Rows[rowIndex].Cells[0].FindControl("ddpBathroom"); 
DropDownList box4 = (DropDownList)GridView2.Rows[rowIndex].Cells[0].FindControl("ddpBedType"); 
TextBox box5 = (TextBox)GridView2.Rows[rowIndex].Cells[0].FindControl("txtPrices"); 
DropDownList box6 = (DropDownList)GridView2.Rows[rowIndex].Cells[0].FindControl("ddpMiniStay"); 
//get the values from the TextBoxes 
//then add it to the collections with a comma "," as the delimited values 
sc.Add("Private Room" + box1.Text + "," + box2.SelectedItem.Value + "," + box3.SelectedItem.Value + "," + box4.SelectedItem.Value + "," + box5.Text + "," + box6.SelectedItem.Value); 
rowIndex++; 
} 

InsertToDb(sc); 

然后

要保存按钮点击室温
private void InsertToDb(StringCollection sc) 
    { 
     string[] splitItems = null; 
     foreach (string item in sc) 
     { 
     //split and save in to db here 
     } 
    } 
+0

谢谢,但它不是我所需要的。网格最初由存储过程填充。用户应该能够更新网格,但不能更新数据库,直到完成所有编辑。此外,他们应该可以添加一个新行,再次不更新数据库,直到点击确定按钮。因此,基本上所有的编辑都应该存储在视图状态或类似的视图状态中,然后从视图状态中读取以在编辑时刷新网格,然后单击OK按钮更新数据库并进行编辑和添加。 – user1801525

相关问题