none
DataGrid中新添加的行,如何写回到SQL数据库中? RRS feed

  • 问题

  • 求助:

    1、在DataGridView显示的数据读取子数据库中。新添加的行,怎么写回保存到数据库中?

    2、如果是在数据库操作类中定义方法的话,该怎么定义这个?

     

    请高人指点!

     


    hi,this is paul!
    2011年7月13日 16:36

答案

  • 实例代码:

    ASPX里可以使用UpdatePanel ,以处理回发,代码如下:

    <asp:UpdatePanel runat="server" ID="UpdatePanel1">
             <ContentTemplate>
              <asp:GridView ID="gvwStockInfo" runat="server" CssClass="printTable" AutoGenerateColumns="False"
               Width="100%" HeaderStyle-CssClass="fieldName" DataSourceID="dataStockInfo" DataKeyNames="SN"
               OnRowDataBound="gvwStockInfo_RowDataBound">
               <Columns>
                <asp:TemplateField HeaderText="序号" Visible="false">
                 <ItemTemplate>
                  <asp:Label runat="server" ID="innerID" Text='<%# Eval("SN") %>'></asp:Label>
                 </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="StockTime" HeaderText="进货时间" ItemStyle-Width="7%" ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="DefrayWay" HeaderText="货款支付方式" ItemStyle-Width="7%" ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="ContractTerm" HeaderText="合同帐期" ItemStyle-Width="7%" ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="StorePosition" HeaderText="铺底额度" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="CreditPosition" HeaderText="信用额度" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="AvailablePosition" HeaderText="可利用额度" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="CurrentQualityStore" HeaderText="当前正品库存" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="CurrentModelMachineStore" HeaderText="当前样机库存" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="CurrentInferiorStore" HeaderText="当前残次库存" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="InferiorRate" HeaderText="残次率" ItemStyle-Width="7%" ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="CurrentUnsalableStore" HeaderText="当前滞销库存" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="Gt120DaysUnsalableAmount" HeaderText="120天以上滞销金额" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="LastWeekSaleAmount" HeaderText="上周销售金额" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:CommandField ButtonType="Link" UpdateText="保存" ShowEditButton="true" ShowDeleteButton="true"
                 HeaderText="操作" ItemStyle-Width="8%" ItemStyle-CssClass="wordNoBreak" />
               </Columns>
               <HeaderStyle CssClass="fieldName" />
              </asp:GridView>
              <table cellpadding="0" cellspacing="0" border="0" width="100%" class="printheadTable"
               style='display: <% =gvwStockInfo.Rows.Count>0 ? "none":"" %>'>
               <colgroup>
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 8%">
               </colgroup>
               <tr class="fieldName">
                <th scope="col">
                 进货时间
                </th>
                <th scope="col">
                 货款支付方式
                </th>
                <th scope="col">
                 合同帐期
                </th>
                <th scope="col">
                 铺底额度
                </th>
                <th scope="col">
                 信用额度
                </th>
                <th scope="col">
                 可利用额度
                </th>
                <th scope="col">
                 当前正品库存
                </th>
                <th scope="col">
                 当前样机库存
                </th>
                <th scope="col">
                 当前残次库存
                </th>
                <th scope="col">
                 残次率
                </th>
                <th scope="col">
                 当前滞销库存
                </th>
                <th scope="col">
                 120天以上滞销金额
                </th>
                <th scope="col">
                 上周销售金额
                </th>
                <th scope="col" style='display: <%= (gvwStockInfo.Columns[gvwStockInfo.Columns.Count - 1].Visible ? "":"none") %>'>
                 操作
                </th>
               </tr>
              </table>
              <asp:LinkButton runat="server" ID="appendBtn" Text="添加" OnClick="appendBtn_Click"></asp:LinkButton>
              <HBEX:CollectionViewStateDataSource runat="server" ID="dataStockInfo">
              </HBEX:CollectionViewStateDataSource>
             </ContentTemplate>
            </asp:UpdatePanel>

    CS事件,保存数据:

    protected void appendBtn_Click(object sender, EventArgs e)
            {
                           int sn = 0;
                if (gvwStockInfo.Rows.Count > 0)
                {
                    sn = int.Parse(gvwStockInfo.DataKeys[gvwStockInfo.Rows.Count - 1].Value.ToString());
                }
                sn++;
                ViewData.Data.StockInfoCollection.Add(new StockInfo(sn));
                gvwStockInfo.EditIndex = gvwStockInfo.Rows.Count;
                gvwStockInfo.DataBind();
            }

     


    天行健,君子当自强不息!
    2011年7月14日 9:03
  • 这里方法很多。

    gridview是绑定到数据源上的,新加入的行是有标记的,在保存的时候将所有新的行插入数据库即可。

    现在有些组件能够自动化做这些。

     

    2.实际上就是一个数据插入表的操作,可以传入一个datarow或者dataset,然后保存。


    family as water
    2011年7月13日 23:34
  • 试试这段代码:

    DataTable dt = new DataTable();
     
        SqlDataAdapter da;
     
        DataSet ds = new DataSet();
     
        private void button2_Click(object sender, EventArgs e)
     
        {
     
          string str = @"Data Source=.\SQLEXPRESS;database=northwind;Integrated Security=True;Connect Timeout=30;User Instance=false";
     
          SqlConnection con = new SqlConnection(str);
     
          con.Open();
     
          string cd = "select * from products";
     
          da = new SqlDataAdapter(cd,con);
     
         
    
          da.Fill(ds, "pro");
     
          dt = ds.Tables[0];
     
          con.Close();
     
          dataGridView1.DataSource = dt;
     
              
     
     
     
        }
     
     
     
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)//或者private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
         
        {
     
          SqlCommandBuilder local_SqlCommandBuilder = new SqlCommandBuilder(da);
     
          local_SqlCommandBuilder.ConflictOption = System.Data.ConflictOption.OverwriteChanges;
     
          da.UpdateCommand = local_SqlCommandBuilder.GetUpdateCommand();
     
          da.Update(((System.Data.DataTable)this.dataGridView1.DataSource));
     
          ((System.Data.DataTable)this.dataGridView1.DataSource).AcceptChanges();
     
        }
    
    


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年7月15日 5:51
    版主

全部回复

  • 这里方法很多。

    gridview是绑定到数据源上的,新加入的行是有标记的,在保存的时候将所有新的行插入数据库即可。

    现在有些组件能够自动化做这些。

     

    2.实际上就是一个数据插入表的操作,可以传入一个datarow或者dataset,然后保存。


    family as water
    2011年7月13日 23:34
  • 实例代码:

    ASPX里可以使用UpdatePanel ,以处理回发,代码如下:

    <asp:UpdatePanel runat="server" ID="UpdatePanel1">
             <ContentTemplate>
              <asp:GridView ID="gvwStockInfo" runat="server" CssClass="printTable" AutoGenerateColumns="False"
               Width="100%" HeaderStyle-CssClass="fieldName" DataSourceID="dataStockInfo" DataKeyNames="SN"
               OnRowDataBound="gvwStockInfo_RowDataBound">
               <Columns>
                <asp:TemplateField HeaderText="序号" Visible="false">
                 <ItemTemplate>
                  <asp:Label runat="server" ID="innerID" Text='<%# Eval("SN") %>'></asp:Label>
                 </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="StockTime" HeaderText="进货时间" ItemStyle-Width="7%" ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="DefrayWay" HeaderText="货款支付方式" ItemStyle-Width="7%" ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="ContractTerm" HeaderText="合同帐期" ItemStyle-Width="7%" ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="StorePosition" HeaderText="铺底额度" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="CreditPosition" HeaderText="信用额度" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="AvailablePosition" HeaderText="可利用额度" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="CurrentQualityStore" HeaderText="当前正品库存" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="CurrentModelMachineStore" HeaderText="当前样机库存" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="CurrentInferiorStore" HeaderText="当前残次库存" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="InferiorRate" HeaderText="残次率" ItemStyle-Width="7%" ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="CurrentUnsalableStore" HeaderText="当前滞销库存" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="Gt120DaysUnsalableAmount" HeaderText="120天以上滞销金额" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:BoundField DataField="LastWeekSaleAmount" HeaderText="上周销售金额" ItemStyle-Width="7%"
                 ItemStyle-CssClass="wordBreak" />
                <asp:CommandField ButtonType="Link" UpdateText="保存" ShowEditButton="true" ShowDeleteButton="true"
                 HeaderText="操作" ItemStyle-Width="8%" ItemStyle-CssClass="wordNoBreak" />
               </Columns>
               <HeaderStyle CssClass="fieldName" />
              </asp:GridView>
              <table cellpadding="0" cellspacing="0" border="0" width="100%" class="printheadTable"
               style='display: <% =gvwStockInfo.Rows.Count>0 ? "none":"" %>'>
               <colgroup>
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 7%">
                <col style="width: 8%">
               </colgroup>
               <tr class="fieldName">
                <th scope="col">
                 进货时间
                </th>
                <th scope="col">
                 货款支付方式
                </th>
                <th scope="col">
                 合同帐期
                </th>
                <th scope="col">
                 铺底额度
                </th>
                <th scope="col">
                 信用额度
                </th>
                <th scope="col">
                 可利用额度
                </th>
                <th scope="col">
                 当前正品库存
                </th>
                <th scope="col">
                 当前样机库存
                </th>
                <th scope="col">
                 当前残次库存
                </th>
                <th scope="col">
                 残次率
                </th>
                <th scope="col">
                 当前滞销库存
                </th>
                <th scope="col">
                 120天以上滞销金额
                </th>
                <th scope="col">
                 上周销售金额
                </th>
                <th scope="col" style='display: <%= (gvwStockInfo.Columns[gvwStockInfo.Columns.Count - 1].Visible ? "":"none") %>'>
                 操作
                </th>
               </tr>
              </table>
              <asp:LinkButton runat="server" ID="appendBtn" Text="添加" OnClick="appendBtn_Click"></asp:LinkButton>
              <HBEX:CollectionViewStateDataSource runat="server" ID="dataStockInfo">
              </HBEX:CollectionViewStateDataSource>
             </ContentTemplate>
            </asp:UpdatePanel>

    CS事件,保存数据:

    protected void appendBtn_Click(object sender, EventArgs e)
            {
                           int sn = 0;
                if (gvwStockInfo.Rows.Count > 0)
                {
                    sn = int.Parse(gvwStockInfo.DataKeys[gvwStockInfo.Rows.Count - 1].Value.ToString());
                }
                sn++;
                ViewData.Data.StockInfoCollection.Add(new StockInfo(sn));
                gvwStockInfo.EditIndex = gvwStockInfo.Rows.Count;
                gvwStockInfo.DataBind();
            }

     


    天行健,君子当自强不息!
    2011年7月14日 9:03
  • 试试这段代码:

    DataTable dt = new DataTable();
     
        SqlDataAdapter da;
     
        DataSet ds = new DataSet();
     
        private void button2_Click(object sender, EventArgs e)
     
        {
     
          string str = @"Data Source=.\SQLEXPRESS;database=northwind;Integrated Security=True;Connect Timeout=30;User Instance=false";
     
          SqlConnection con = new SqlConnection(str);
     
          con.Open();
     
          string cd = "select * from products";
     
          da = new SqlDataAdapter(cd,con);
     
         
    
          da.Fill(ds, "pro");
     
          dt = ds.Tables[0];
     
          con.Close();
     
          dataGridView1.DataSource = dt;
     
              
     
     
     
        }
     
     
     
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)//或者private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
         
        {
     
          SqlCommandBuilder local_SqlCommandBuilder = new SqlCommandBuilder(da);
     
          local_SqlCommandBuilder.ConflictOption = System.Data.ConflictOption.OverwriteChanges;
     
          da.UpdateCommand = local_SqlCommandBuilder.GetUpdateCommand();
     
          da.Update(((System.Data.DataTable)this.dataGridView1.DataSource));
     
          ((System.Data.DataTable)this.dataGridView1.DataSource).AcceptChanges();
     
        }
    
    


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年7月15日 5:51
    版主