如何保存gridview表中的每一行值?

时间:2015-05-09 12:16:01

标签: c# gridview

如何将gridview的每一行值插入到database.I使用下面的代码,但null值传递给字符串。

foreach (GridViewRow gvrow in GridView1.Rows)
            {
                con.Open();

                string datetime = Request.Form["txtdate"];
                str += GridView1.DataKeys[gvrow.RowIndex].Value.ToString() + ',';
                Id  += gvrow.Cells[0].Text;
                fName += gvrow.Cells[1].Text;
                FaName  += gvrow.Cells[2].Text;
                LName  += gvrow.Cells[3].Text;
                attendance  += gvrow.Cells[4].Text;
                remarks  += gvrow.Cells[5].Text;
                SqlCommand cmd = new SqlCommand("INSERT INTO [first].[dbo].[Staff_Attendance]([Id],[Department],[Date],[First_name],[Father_name],[Last_name],[Attendance],[Remarks]) VALUES(@Id, @Department, @Date, @First_name, @Father_name, @Last_name, @Attendance, @Remarks)", con);
                cmd.Parameters.AddWithValue("@Id", Id);
                cmd.Parameters.AddWithValue("@Department", DropDownList1.SelectedItem .ToString ());
                cmd.Parameters.AddWithValue("@Date", datetime.ToString());

                cmd.Parameters.AddWithValue("@First_name", fName);
                cmd.Parameters.AddWithValue("@Father_name", FaName);
                cmd.Parameters.AddWithValue("@Last_name", LName);
                cmd.Parameters.AddWithValue("@Attendance", attendance);
                cmd.Parameters.AddWithValue("@Remarks", remarks);

                cmd.ExecuteNonQuery();
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Data Have Been Saved')", true);
                con.Close();
            }

Gridview代码: -

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" 
                    PageSize="5" AutoGenerateColumns="False" DataKeyNames ="srno">
                    <PagerSettings FirstPageText="First" LastPageText="Last" 
                        Mode="NumericFirstLast" PageButtonCount="5" />
                        <Columns >
                        <asp:TemplateField HeaderText="Sr.No.">


    <ItemTemplate>
  <asp:Label ID="lblsrno" runat="server" Text='<%#Eval("srno") %>'>
    </asp:Label>

    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="First Name">


    <ItemTemplate>
  <asp:Label ID="lblFname" runat="server" Text='<%#Eval("first_name") %>'>
    </asp:Label>

    </ItemTemplate>
    </asp:TemplateField>

       <asp:TemplateField HeaderText="Father Name">


    <ItemTemplate>
  <asp:Label ID="lblFaName" runat="server" Text='<%#Eval("father_name") %>'>
    </asp:Label>

    </ItemTemplate>
    </asp:TemplateField>

       <asp:TemplateField HeaderText="Last Name">


    <ItemTemplate>
  <asp:Label ID="lblLName" runat="server" Text='<%#Eval("last_name") %>'>
    </asp:Label>

    </ItemTemplate>
    </asp:TemplateField>

       <asp:TemplateField HeaderText="Attendance">


    <ItemTemplate>
 <asp:DropDownList ID="ddlDesignation" runat="server" Width ="80px">
<asp:ListItem Text ="--Select--" ></asp:ListItem>
<asp:ListItem Text ="P"></asp:ListItem>
<asp:ListItem Text ="A"></asp:ListItem>
                        </asp:DropDownList>


    </ItemTemplate>
    </asp:TemplateField>


    <asp:TemplateField HeaderText="Remarks">


    <ItemTemplate>
 <asp:DropDownList ID="ddlRemark" runat="server" Width ="80px">
<asp:ListItem Text ="--Select--" ></asp:ListItem>
<asp:ListItem Text ="Paid Leave"></asp:ListItem>
<asp:ListItem Text ="Unpaid Leave"></asp:ListItem>
                        </asp:DropDownList>


    </ItemTemplate>
    </asp:TemplateField>
                        </Columns>
                </asp:GridView>

在数据库中,它存储了空值

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以尝试使用以下代码,编写示例代码,

foreach (GridViewRow row in GridView1.Rows) {

    Label lblFname = (Label)row.FindControl("lblFname");
    Label lblFaName = (Label)row.FindControl("lblFaName");
    Label lblLName = (Label)row.FindControl("lblLName");
    DropDownList ddl_att = (DropDownList)row.FindControl("ddlDesignation");
    DropDownList ddl_rmk = (DropDownList)row.FindControl("ddlRemark");

    dataInsert(lblFname.Text,lblFaName.Text,ddl_att.SelectedValue);

}


public void dataInsert(string First_name,string Father_name,string Attendance)
{
 using (SqlConnection con = new SqlConnection(conn.ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "yourInsertQuery";
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@First_name", First_name);
            cmd.Parameters.AddWithValue("@Father_name", Father_name);
            cmd.Parameters.AddWithValue("@Attendance", Attendance);
                ...
                ...     
            con.Open();
            cmd.ExecuteNonQuery();
        }
        con.Close();
    }
}

此外,您可以将数据保存到datatable中,如果您的DataTable架构和表架构匹配,那么使用SqlBulkCopy可以批量插入数据库。

答案 1 :(得分:0)

首先您可以在gridview中找到控件然后获取Text。可能是你的问题会解决。

            fName += ((Label)gvrow.Cells[1].FindControl("lblFname")).Text;
            FaName  +=((Label)gvrow.Cells[2].FindControl("lblFaName").Text);
            LName  += ((Label)gvrow.Cells[3].FindControl("lblLName")).Text;
            attendance  +=((DropDownList)gvrow.Cells[4].FindControl("ddlDesignation")).SelectedItem.Text;
            remarks  += ((DropDownList)gvrow.Cells[5].FindControl("ddlRemark")).SelectedItem.Text;