如何将行整个gridview控件存储到数据库 [英] How to store the row entire gridview controls to database

查看:44
本文介绍了如何将行整个gridview控件存储到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

this is my code what i can do to save the rows entire gridview to databse
_________________________________________________________________


<div>
            <asp:GridView ID="grvStudentDetails" runat="server" ShowFooter="True" AutoGenerateColumns="False"

                CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="grvStudentDetails_RowDeleting"

                Width="97%" Style="text-align: left" OnRowDataBound="gv_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="RowNumber" HeaderText="SNo" />
                    <asp:TemplateField HeaderText="Student Name">
                        <ItemTemplate>
                            <asp:TextBox ID="txtName" runat="server" MaxLength="50"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"

                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Student Age">
                        <ItemTemplate>
                            <asp:TextBox ID="txtAge" runat="server" MaxLength="3" Width="66px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtAge"

                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Student Address">
                        <ItemTemplate>
                            <asp:TextBox ID="txtAddress" runat="server" Height="55px" TextMode="MultiLine"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAddress"

                                ErrorMessage="*" SetFocusOnError="True"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Gender">
                        <ItemTemplate>
                            <asp:RadioButtonList ID="RBLGender" runat="server" RepeatDirection="Horizontal">
                                <asp:ListItem Value="M">Male</asp:ListItem>
                                <asp:ListItem Value="F">Female</asp:ListItem>
                            </asp:RadioButtonList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="RBLGender"

                                ErrorMessage="*"></asp:RequiredFieldValidator>&nbsp;
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Qualification">
                        <ItemTemplate>
                            <asp:DropDownList ID="drpQualification" runat="server">
                                <asp:ListItem>Select</asp:ListItem>
                                <asp:ListItem Value="G">Graduate</asp:ListItem>
                                <asp:ListItem Value="P">Post Graduate</asp:ListItem>
                            </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="drpQualification"

                                ErrorMessage="*" InitialValue="Select"></asp:RequiredFieldValidator>
                        </ItemTemplate>
                        <FooterStyle HorizontalAlign="Right" />
                        <FooterTemplate>
                            <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowDeleteButton="True" />
                </Columns>
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#EFF3FB" />
                <EditRowStyle BackColor="#2461BF" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
            <br />
            <center>
                <asp:Button ID="btnSave" runat="server" Text="Save Data" OnClick="btnSave_Click" />
            </center>
        </div>

and my code behind is :

<blockquote class="quote"><div class="op">Quote:</div> protected void btnSave_Click(object sender, EventArgs e)
    {


        con.Open();
        tran = con.BeginTransaction();
        cmd.Transaction = tran;
        string slno = null;

            foreach (GridViewRow g1 in grvStudentDetails.Rows)
                {

                    //string id = ((TextBox)g1.Cells[0].FindControl("txName")).Text;
                    string id = ((TextBox)g1.FindControl("TextBox1")).Text;
                    string name = (g1.FindControl("txAge") as TextBox).Text;
                    string price = (g1.FindControl("txtAddress") as TextBox).Text;
                    string description = (g1.FindControl("RBLGender") as Label).Text;

                    string query = "insert into SU_Seeker_Qualifications (Type,Join_Date,Graduated_Date,Univerisity) values(" + id + ",'" + name + "'," + price + ",'" + description + "')";
                 slno = id;
                    cmd.CommandText = query;
                    cmd.ExecuteNonQuery();
                }
                tran.Commit();
                lblMessage.Text = "Records inserted successfully";
        //}
        con.Close();</blockquote>

推荐答案

The easiest way to create and save data with DataGridView is to make a DataSet and build it by using Server Explorer to open your SQL database and add tables.

Visual Studio will generate all classes and methods needed.

You will need to learn about DataSets, TableAdapters etc.



After making the dataset, build your app and the DataSet controls will be available in your ToolBox when in designer view.



Then add your DataGridView and select the columns and link them to the dataset tables.

The designer will generate data binding and through them you can do CRUD with your data[^].
The easiest way to create and save data with DataGridView is to make a DataSet and build it by using Server Explorer to open your SQL database and add tables.
Visual Studio will generate all classes and methods needed.
You will need to learn about DataSets, TableAdapters etc.

After making the dataset, build your app and the DataSet controls will be available in your ToolBox when in designer view.

Then add your DataGridView and select the columns and link them to the dataset tables.
The designer will generate data binding and through them you can do CRUD with your data[^].


这篇关于如何将行整个gridview控件存储到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆