如何在单独的按钮单击事件中访问gridview数据?

时间:2015-07-09 09:00:34

标签: c# asp.net gridview

我的要求是我需要在按钮点击事件中访问网格视图数据。 我有名为gvData的网格视图,并有名为Update的按钮。 点击更新按钮,我将用户输入网格的数据保存到DB。 请建议我完成这项工作。代码片段会有很大帮助。 我在页面加载时填充gridview:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DataTable dt;
                String SQL = "SELECT * FROM tbl_Class";


                string sConstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(sConstr))
                {
                    using (SqlCommand comm = new SqlCommand(SQL, conn))
                    {
                        conn.Open();
                        using (SqlDataAdapter da = new SqlDataAdapter(comm))
                        {
                            dt = new DataTable();
                            da.Fill(dt);
                        }
                    }
                }

                gvMarks.DataSource = dt;
                gvMarks.DataBind();

            }
        }
<asp:GridView ID="gvMarks" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name"/>
                    <asp:TemplateField HeaderText="One">

                        <ItemTemplate>

                            <asp:TextBox ID="txt1" runat="server" Text='<%# Bind("One") %>'></asp:TextBox>

                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Two">

                        <ItemTemplate>
                            <asp:TextBox ID="txt2" runat="server" Text='<%# Bind("TWO") %>'></asp:TextBox>

                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Three">

                        <ItemTemplate>
                            <asp:TextBox ID="txtThree" runat="server" Text='<%# Bind("THREE") %>'></asp:TextBox>


                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Four">

                        <ItemTemplate>

                            <asp:TextBox ID="txt4" runat="server" Text='<%# Bind("FOUR") %>'></asp:TextBox>

                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="FIVE">

                        <ItemTemplate>
                            <asp:TextBox ID="txtFive" runat="server" Text='<%# Bind("FIVE") %>'></asp:TextBox>

                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="SIX">

                        <ItemTemplate>
                            <asp:TextBox ID="txt6" runat="server" Text='<%# Bind("SIX") %>'></asp:TextBox>

                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:Button ID="btnUpdae" runat="server" Text="Update Marks" OnClick="btnUpdae_Click" />


    protected void btnUpdae_Click(object sender, EventArgs e)
        {
             //Need to acces the grid view rows and colums here
         }

3 个答案:

答案 0 :(得分:1)

通过foreach或for循环逐个获取行并保存数据。 Click This to know more about save the gridview data to your database

答案 1 :(得分:0)

试试这个。

protected void btnUpdae_Click(object sender, EventArgs e)
    {
            int rowIndex = 0;
            StringCollection sc = new StringCollection();
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        //extract the TextBox values
                        TextBox box1 = (TextBox)gvMarks.Rows[rowIndex].Cells[0].FindControl("txt1");
                        TextBox box2 = (TextBox)gvMarks.Rows[rowIndex].Cells[1].FindControl("txt2");
                        TextBox box3 = (TextBox)gvMarks.Rows[rowIndex].Cells[2].FindControl("txt3");
                        TextBox box4 = (TextBox)gvMarks.Rows[rowIndex].Cells[3].FindControl("txt4");
                        TextBox box5 = (TextBox)gvMarks.Rows[rowIndex].Cells[4].FindControl("txt5");
                        TextBox box6 = (TextBox)gvMarks.Rows[rowIndex].Cells[5].FindControl("txt6");

                        //get the values from the TextBoxes
                        //then add it to the collections with a comma "," as the delimited values
                        sc.Add(box1.Text + "," + box2.Text + "," + box3.Text+ "," + box4.Text + "," + box5.Text+ "," + box6.Text);
                        rowIndex++;
                    }
                    //Call the method for executing inserts or update
                }
            }
    }

答案 2 :(得分:0)

您可以采用以下方法 -

TextBox txt=(TextBox)gvMarks.Rows[i].Cells[0].FindControl("txt1");
string txtOne= txt.Text;

您可以将此用于for循环和所有文本框。然后将字符串变量(如txtOne)(上面使用的)中的数据插入/更新到表中。然后再次绑定网格。您可以点击以下链接获取其他解决方案。

http://forums.asp.net/t/1647616.aspx?Get%20Value%20of%20TextBox%20inside%20the%20GridView%20in%20c%20

我希望这可以让你的工作完成。