2013-10-17 41 views
0

我有Gridview有多個行與文本框和下拉列表...我想插入數據與多個行的文本框和下拉列表中的數據一次存儲過程。它是如何可能..請幫助我,如何從gridview插入多行到數據庫

<asp:gridview ID="Gridview1" runat="server" ShowFooter="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"> 
    <Columns> 
    <asp:BoundField DataField="RowNumber" HeaderText="Row Number" /> 
     <asp:TemplateField HeaderText="First Name"> 
      <ItemTemplate> 
       <asp:TextBox ID="txtFirstName" runat="server" CssClass="textbox"></asp:TextBox> 
      </ItemTemplate> 
    </asp:TemplateField> 
<asp:TemplateField HeaderText="Last Name"> 
    <ItemTemplate> 
     <asp:TextBox ID="txtLastName" runat="server" CssClass="textbox"></asp:TextBox> 
    </ItemTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="Date"> 
<ItemTemplate> 
    <asp:TextBox ID="txtDate" runat="server" CssClass="textbox"></asp:TextBox> 
    </ItemTemplate></asp:TemplateField> 
    <asp:TemplateField> 
    <ItemTemplate> 
    <asp:DropDownList ID="ddlAgeRange" runat="server"> 
    <asp:ListItem Value="-1">Age Range</asp:ListItem> 
    </asp:DropDownList> 
    </ItemTemplate></asp:TemplateField> 
    <asp:TemplateField> 
    <ItemTemplate> 
    <asp:DropDownList ID="ddlRelationship" runat="server"> 
    <asp:ListItem Value="-1">Relationship</asp:ListItem> 
    </asp:DropDownList> 
    </ItemTemplate></asp:TemplateField> 
    <asp:TemplateField> 
    <ItemTemplate> 
     <asp:DropDownList ID="ddlGender" runat="server"> 
      <asp:ListItem Value="-1">Gender</asp:ListItem> 
      </asp:DropDownList> 
     </ItemTemplate></asp:TemplateField> 
     </Columns> 
     <FooterStyle BackColor="White" ForeColor="#000066" /> 
     <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> 
     <RowStyle ForeColor="#000066" /> 
     <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> 
     <SortedAscendingCellStyle BackColor="#F1F1F1" /> 
     <SortedAscendingHeaderStyle BackColor="#007DBB" /> 
     <SortedDescendingCellStyle BackColor="#CAC9C9" /> 
     <SortedDescendingHeaderStyle BackColor="#00547E" /> 
     </asp:gridview> 
+0

你試過? – nrsharma

+0

我有兩個鏈接按鈕後gridview我有邏輯來增加和減少,當點擊鏈接按鈕,現在我想回顧價值當用戶輸入文本的多行文本框到數據庫 – user2867963

+0

你可以做一個for或foreach循環點擊按鈕。迭代槽gridview中的所有行,並從gridview中的控件採取值supp的價值存儲過程的參數和插入... enjoye :) – nrsharma

回答

0
private void userInsert() 
{ 
    if (MessageBox.Show("Do you want to add the new data ?", "Confirm ", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes) 
    { 
     cmd.Connection = con; 
     cmd.CommandType = CommandType.Text; 
     try 
     { 
      foreach (GridViewRow dRow in userDataGridView.Rows) 
      { 
       cmd.CommandText = string.Format(
        "insert into users(first_name,last_name,default_rate,default_location,spi_user_id,nickname) values('{0}','{1}',{2},{3},{4},'{5}')", 
        userDataGridView.CurrentRow.Cells[0].Value.ToString(), 
        userDataGridView.CurrentRow.Cells[1].Value.ToString(), 
        userDataGridView.CurrentRow.Cells[2].Value.ToString(), 
        locationID2ComboBox.SelectedValue, 
        userDataGridView.CurrentRow.Cells[4].Value.ToString(), 
        userDataGridView.CurrentRow.Cells[5].Value.ToString()); 
       con.Open(); 
       cmd.ExecuteNonQuery(); 
      } 
      MessageBox.Show("Your data has been added successfully ", "Saved info", ageBoxButtons.OK, MessageBoxIcon.Information); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      con.Close(); 
      userSelect(); 
     } 
    } 
} 
+0

每個人都可以使用它作爲從griedview到數據庫插入多個數據。我想,這是非常每個人都很簡單。 –

相關問題