本文介绍了如何将GridView TextBox的值存储到SQL数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 < asp:GridView ID = GridView1 runat = server AutoGenerateColumns = False BackColor = #DEBA84 BorderColor = #DEBA84 BorderStyle = 无 BorderWidth = 1px CellPadding = 3 CellSpacing = 2 DataKeyNames = Qid DataSourceID = SqlDataSource1 onrowcommand = GridView1_RowCommand > < 列 > < asp:CommandField ShowSelectButton = True / > < asp:BoundField DataField = Qid HeaderText = Qid InsertVisible = 错误 ReadOnly = True SortExpression = Qid / > < asp:BoundField DataField = 问题 HeaderText = 问题 SortExpression = 问题 / > < asp:BoundField DataField = ans1 HeaderText = ans1 SortExpression = ans1 / > < asp:BoundField DataField = ans2 HeaderText = ans2 SortExpression = ans2 / > < asp:BoundField DataField = ans3 HeaderText = ans3 SortExpression = ans3 / > < asp:BoundField DataField = ans4 HeaderText = ans4 SortExpression = ans4 / > < asp:BoundField DataField = cans HeaderText = cans SortExpression = cans / > < asp:TemplateField > < ItemTemplate > < asp:TextBox ID = TextBox8 runat = server > < / asp:TextBox > < / ItemTemplate > < / asp:TemplateField > < / Columns > < FooterStyle BackColor = #F7DFB5 ForeColor = #8C4510 / > < HeaderStyle BackColor = #A55129 字体粗体 = True ForeColor = 白色 / > < PagerStyle ForeColor = #8C4510 Horizo​​ntalAlign = 中心 / > < RowStyle BackColor = #FFF7E7 ForeColor = #8C4510 / > < SelectedRowStyle BackColor = #738A9C 字体粗体 = 真 ForeColor = 白色 / > < SortedAscendingCellStyle BackColor = #FFF1D4 / > < SortedAscendingHeaderStyle BackColor = #B95C30 / > < SortedDescendingCellStyle BackColor = #F1E5CE / > < SortedDescendingHeaderStyle BackColor = #93451F / > < / asp:GridView > 解决方案 参考:插入,更新,删除Gridview ......简单方法 [ ^ ] for ( int i = 0 ; i < dataGridView1.Rows.Count - 1 ; i ++) {< pre lang = cs> String r = INSERT INTO Table_name(invoiceno,srno,itmdescription,)VALUES(@ invoiceno,@ srno,@ itmdescription) ; cmd = new SqlCommand(r,cn); cmd.Parameters.AddWithValue( @ invoiceno,txt_invono.Text); cmd.Parameters.AddWithValue( @ srno,dataGridView1.Rows [i]。单元格[ 0 ]。值); cmd.Parameters.AddWithValue( @ itmdescription,dataGridView1.Rows [i]。单元格[ 1 ]。值); cmd.executenonquery(); } .aspx page < 表格 id = form1 runat = 服务器 > < asp:ScriptManager ID = ScriptManager2 runat = 服务器 > < / asp:ScriptManager > < asp:UpdatePanel ID = updpnl runat = 服务器 > < ContentTemplate > < asp:GridView ID = grddata runat = server AutoGenerateColumns = false OnRowUpdating = grddata_RowUpdating OnRowEditing = grddata_RowEditing OnRowDeleting = grddata_RowDeleting OnRowCancelingEdit = grddata_RowCancelingEdit > < 列 > < asp:TemplateField > < HeaderTemplate > < 表格 > < tr > < td > < asp:Label ID = lblid 宽度 = 200px runat = server 文字 = Id > < / asp:Label > < / td > < td > < asp:标签 ID = lblname 宽度 = 200px runat = server 文本 = 名称 > < / asp:Label > < / td > < ; td > < asp:标签 ID = lblsalary runat = server 宽度 = 200px 文本 = 薪水 > < / asp:Label > < / td > < / tr > < / table > < / HeaderTemplate > < ItemTemplate > < table > < tr > < td > < asp:Label ID = lblid 宽度 = 200px 文字 = runat=\"server\"></asp:Label> </td> <td> <asp:Label ID=\"lblname\" Width=\"200px\" Text=' <%# Eval(\"name\") %>' runat=\"server\"></asp:Label> </td> <td> <asp:Label ID=\"lblsalary\" Width=\"200px\" runat=\"server\" Text='<%# Eval(\"salary\") %>'></asp:Label> </td> <td> <asp:LinkButton ID=\"lnkedit\" CommandName=\"Edit\" OnClientClick=\"About.aspx.target='_blank';\" Text=\"Edit\" CommandArgument='<%# Eval(\"id\") %>' runat=\"server\"></asp:LinkButton> </td> <td> <asp:LinkButton ID=\"lnkdelete\" Text=\"Delete\" CommandName=\"Delete\" CommandArgument='<%# Eval(\"id\") %>' runat=\"server\"></asp:LinkButton> </td> <td> <asp: LinkButton ID=\"redirect\" runat=\"server\" Text=\"Redirect\" OnClientClick=\"About.aspx\" target=\"_blank\"></asp:LinkButton> </td> </tr> </table> </ItemTemplate> <EditItemTemplate> <tr> <td> <asp:Label ID=\"lblid\" Width=\"200px\" Text='<%# Eval(\"id\") %>' runat=\"server\"></asp:Label> </td> <td> <asp:TextBox ID=\"txtname\" runat=\"server\"></asp:TextBox> </td> <td> <asp:TextBox ID=\"txtsalary\" runat=\"server\"></asp:TextBox> </td> <td> <asp:LinkButton ID=\"lnkupdate\" CommandName=\"Update\" Text=\"Update\" CommandArgument='<%# Eval(\"id\") %>' runat=\"server\"></asp:LinkButton> </td> <td> <asp:LinkButton ID=\"lnkCancel\" CommandName=\"Cancel\" Text=\"Cancel\" CommandArgument='<%# Eval(\"id\") %>' runat=\"server \"></asp:LinkButton> </td> </tr> </EditItemTemplate> </asp:TemplateField> </Columns> <EmptyDataTemplate> No Data Available </EmptyDataTemplate> </asp:GridView> </ContentTemplate> </asp:UpdatePanel> </form> .cs page using System; using System.Configurati on; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; using System.Net; using System.Text; public partial class Default2 : System.Web.UI.Page { SqlConnection con = new SqlConnection(@\"server=ADMIN-PC\NEERAJ;database=master;integrated security=true;\"); protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { showdata(); } } //private void PDF_Export() //{ // Response.ContentType = \"application/pdf\"; $b $b // Response.AddHeader(\"content-disposition\", // \"attachment;filename=GridViewExport.pdf\"); // Response.Cache.SetCacheability(HttpCacheability.NoCache); // StringWriter sw = new StringWriter(); // HtmlTextWriter hw = new HtmlTextWriter(sw); // GridView1.AllowPaging = false; // GridView1.DataBind(); // GridView1.RenderControl(hw); // StringReader sr = new StringReader(sw.ToString()); // Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); // HTMLWorker htmlparser = new HTMLWorker(pdfDoc); // PdfWriter.GetInstance(pdfDoc, Response.OutputStream); // pdfDoc.Open(); // htmlparser.Parse(sr); // pdfDoc.Close(); // Response.Write(pdfDoc); // Response.End(); //} public void showdata() { SqlDataAdapter da=new SqlDataAdapter(\"select * from empdata\",con); DataSet ds=new DataSet(); da.Fill(ds); grddata.DataSource=ds; grddata.DataBind(); } protected void grddata_RowEditing(object sender, GridViewEditEventArgs e) { //grddata.EditIndex = -1; //Label name = (Label)grddata.Rows[0].Cells[1].FindControl(\"lblname\"); //TextBox salary = (TextBox)grddata.Rows[0].Cells[2].FindControl(\"txtsalary\"); grddata.EditIndex = e.NewEditIndex; //LinkButton edit = (LinkButton)grddata.Rows[0].FindControl(\"lnkedit\"); // //Response.Redirect(\"About.aspx?name=neeraj&salary=1200\"); //string url = \"About.aspx?name=neeraj&salary=1200\"; //edit.OnClientClick = \"wind ow.open(’\" + url + \"’); return false;\"; } protected void grddata_RowDeleting(object sender, GridViewDeleteEventArgs e) { //LinkButton edit = (LinkButton)grddata.Rows[0].FindControl(\"lnkedit\"); Label name = (Label)grddata.Rows[e.RowIndex].FindControl(\"lblname\"); //Label salary = (Label)grddata.Rows[0].FindControl(\"lblsal\"); Label id = (Label)grddata.Rows[e.RowIndex].FindControl(\"lblid\"); ViewState[\"id\"] = id.Text; //string id = grddata.DataKeys // [e.Item.ItemIndex].ToString(); //SqlDataAdapter da = new SqlDataAdapter(\"UPDATE empdata SET name =’\" + name + \"’ and salary=\" + salary + \" \", con); SqlCommand cmd; cmd = new SqlCommand(\"delete from empdata where id=\"+ViewState[\"id\"]+\" \", con); cmd.CommandType = CommandType.Text; con.Open(); cmd.ExecuteNonQuery(); showdata(); con.Close(); } protected void grddata_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { grddata.EditIndex = -1; showdata(); } protected void grddata_RowUpdating(object sender, GridViewUpdateEventArgs e) { //string id = grddata.DataKeys[e.Item.ItemIndex].ToString(); TextBox name = (TextBox)grddata.Rows[0].FindControl(\"txtname\"); TextBox salary = (TextBox)grddata.Rows[0].FindControl(\"txtsalary\"); //Label id = (Label)grddata.Rows[e.RowIndex].FindControl(\"lblid.text\"); //SqlDataAdapter da = new SqlDataAd apter(\"UPDATE empdata SET name =’\" + name + \"’ and salary=\" + salary + \" \", con); SqlCommand cmd; cmd = new SqlCommand(\"UPDATE empdata SET name=’\" + name.Text + \"’,salary=\" + salary.Text + \" \", con); cmd.CommandType = CommandType.Text; con.Open(); cmd.ExecuteNonQuery(); con.Close(); grddata.EditIndex = -1; showdata(); } public void sendmail() { StringBuilder sb = new StringBuilder(); } }<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataKeyNames="Qid" DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand"> <Columns> <asp:CommandField ShowSelectButton="True" /> <asp:BoundField DataField="Qid" HeaderText="Qid" InsertVisible="False" ReadOnly="True" SortExpression="Qid" /> <asp:BoundField DataField="Question" HeaderText="Question" SortExpression="Question" /> <asp:BoundField DataField="ans1" HeaderText="ans1" SortExpression="ans1" /> <asp:BoundField DataField="ans2" HeaderText="ans2" SortExpression="ans2" /> <asp:BoundField DataField="ans3" HeaderText="ans3" SortExpression="ans3" /> <asp:BoundField DataField="ans4" HeaderText="ans4" SortExpression="ans4" /> <asp:BoundField DataField="cans" HeaderText="cans" SortExpression="cans" /> <asp:TemplateField> <ItemTemplate> <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#FFF1D4" /> <SortedAscendingHeaderStyle BackColor="#B95C30" /> <SortedDescendingCellStyle BackColor="#F1E5CE" /> <SortedDescendingHeaderStyle BackColor="#93451F" /> </asp:GridView> 解决方案 Refer: Insert, Update, Delete with Gridview ... Simple Way[^]for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) {<pre lang="cs">String r = "INSERT INTO Table_name(invoiceno,srno, itmdescription, ) VALUES (@invoiceno,@srno, @itmdescription)";cmd = new SqlCommand(r, cn);cmd.Parameters.AddWithValue("@invoiceno", txt_invono.Text);cmd.Parameters.AddWithValue("@srno", dataGridView1.Rows[i].Cells[0].Value);cmd.Parameters.AddWithValue("@itmdescription",dataGridView1.Rows[i].Cells[1].Value);cmd.executenonquery();}.aspx page<form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager2" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="updpnl" runat="server"> <ContentTemplate> <asp:GridView ID="grddata" runat="server" AutoGenerateColumns="false" OnRowUpdating="grddata_RowUpdating" OnRowEditing="grddata_RowEditing" OnRowDeleting="grddata_RowDeleting" OnRowCancelingEdit="grddata_RowCancelingEdit"> <Columns> <asp:TemplateField> <HeaderTemplate> <table> <tr> <td> <asp:Label ID="lblid" Width="200px" runat="server" Text="Id"></asp:Label> </td> <td> <asp:Label ID="lblname" Width="200px" runat="server" Text="Name"></asp:Label> </td> <td> <asp:Label ID="lblsalary" runat="server" Width="200px" Text="Salary"></asp:Label> </td> </tr> </table> </HeaderTemplate> <ItemTemplate> <table> <tr> <td> <asp:Label ID="lblid" Width="200px" Text= runat="server"></asp:Label> </td> <td> <asp:Label ID="lblname" Width="200px" Text='<%# Eval("name") %>' runat="server"></asp:Label> </td> <td> <asp:Label ID="lblsalary" Width="200px" runat="server" Text='<%# Eval("salary") %>'></asp:Label> </td> <td> <asp:LinkButton ID="lnkedit" CommandName="Edit" OnClientClick="About.aspx.target='_blank';" Text="Edit" CommandArgument='<%# Eval("id") %>' runat="server"></asp:LinkButton> </td> <td> <asp:LinkButton ID="lnkdelete" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("id") %>' runat="server"></asp:LinkButton> </td> <td> <asp:LinkButton ID="redirect" runat="server" Text="Redirect" OnClientClick="About.aspx" target="_blank"></asp:LinkButton> </td> </tr> </table> </ItemTemplate> <EditItemTemplate> <tr> <td> <asp:Label ID="lblid" Width="200px" Text='<%# Eval("id") %>' runat="server"></asp:Label> </td> <td> <asp:TextBox ID="txtname" runat="server"></asp:TextBox> </td> <td> <asp:TextBox ID="txtsalary" runat="server"></asp:TextBox> </td> <td> <asp:LinkButton ID="lnkupdate" CommandName="Update" Text="Update" CommandArgument='<%# Eval("id") %>' runat="server"></asp:LinkButton> </td> <td> <asp:LinkButton ID="lnkCancel" CommandName="Cancel" Text="Cancel" CommandArgument='<%# Eval("id") %>' runat="server"></asp:LinkButton> </td> </tr> </EditItemTemplate> </asp:TemplateField> </Columns> <EmptyDataTemplate> No Data Available </EmptyDataTemplate> </asp:GridView> </ContentTemplate> </asp:UpdatePanel> </form>.cs pageusing System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;using System.Net;using System.Text;public partial class Default2 : System.Web.UI.Page{ SqlConnection con = new SqlConnection(@"server=ADMIN-PC\NEERAJ;database=master;integrated security=true;"); protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { showdata(); } } //private void PDF_Export() //{ // Response.ContentType = "application/pdf"; // Response.AddHeader("content-disposition", // "attachment;filename=GridViewExport.pdf"); // Response.Cache.SetCacheability(HttpCacheability.NoCache); // StringWriter sw = new StringWriter(); // HtmlTextWriter hw = new HtmlTextWriter(sw); // GridView1.AllowPaging = false; // GridView1.DataBind(); // GridView1.RenderControl(hw); // StringReader sr = new StringReader(sw.ToString()); // Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); // HTMLWorker htmlparser = new HTMLWorker(pdfDoc); // PdfWriter.GetInstance(pdfDoc, Response.OutputStream); // pdfDoc.Open(); // htmlparser.Parse(sr); // pdfDoc.Close(); // Response.Write(pdfDoc); // Response.End(); //} public void showdata() { SqlDataAdapter da=new SqlDataAdapter("select * from empdata",con); DataSet ds=new DataSet(); da.Fill(ds); grddata.DataSource=ds; grddata.DataBind(); } protected void grddata_RowEditing(object sender, GridViewEditEventArgs e) { //grddata.EditIndex = -1; //Label name = (Label)grddata.Rows[0].Cells[1].FindControl("lblname"); //TextBox salary = (TextBox)grddata.Rows[0].Cells[2].FindControl("txtsalary"); grddata.EditIndex = e.NewEditIndex; //LinkButton edit = (LinkButton)grddata.Rows[0].FindControl("lnkedit"); // //Response.Redirect("About.aspx?name=neeraj&salary=1200"); //string url = "About.aspx?name=neeraj&salary=1200"; //edit.OnClientClick = "window.open('" + url + "'); return false;"; } protected void grddata_RowDeleting(object sender, GridViewDeleteEventArgs e) { //LinkButton edit = (LinkButton)grddata.Rows[0].FindControl("lnkedit"); Label name = (Label)grddata.Rows[e.RowIndex].FindControl("lblname"); //Label salary = (Label)grddata.Rows[0].FindControl("lblsal"); Label id = (Label)grddata.Rows[e.RowIndex].FindControl("lblid"); ViewState["id"] = id.Text; //string id = grddata.DataKeys // [e.Item.ItemIndex].ToString(); //SqlDataAdapter da = new SqlDataAdapter("UPDATE empdata SET name ='" + name + "' and salary=" + salary + " ", con); SqlCommand cmd; cmd = new SqlCommand("delete from empdata where id="+ViewState["id"]+" ", con); cmd.CommandType = CommandType.Text; con.Open(); cmd.ExecuteNonQuery(); showdata(); con.Close(); } protected void grddata_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { grddata.EditIndex = -1; showdata(); } protected void grddata_RowUpdating(object sender, GridViewUpdateEventArgs e) { //string id = grddata.DataKeys[e.Item.ItemIndex].ToString(); TextBox name = (TextBox)grddata.Rows[0].FindControl("txtname"); TextBox salary = (TextBox)grddata.Rows[0].FindControl("txtsalary"); //Label id = (Label)grddata.Rows[e.RowIndex].FindControl("lblid.text"); //SqlDataAdapter da = new SqlDataAdapter("UPDATE empdata SET name ='" + name + "' and salary=" + salary + " ", con); SqlCommand cmd; cmd = new SqlCommand("UPDATE empdata SET name='" + name.Text + "',salary=" + salary.Text + " ", con); cmd.CommandType = CommandType.Text; con.Open(); cmd.ExecuteNonQuery(); con.Close(); grddata.EditIndex = -1; showdata(); } public void sendmail() { StringBuilder sb = new StringBuilder(); } } 这篇关于如何将GridView TextBox的值存储到SQL数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 00:43