对GridView单元格数据进行一次性保存到数据库中 批量保存GridView数据

6 篇文章 0 订阅
 前台:.aspx页面的GRIDVIEW

 

 <asp:Button ID="btnSave" runat="server" Text="保存修改" οnclick="btnSave_Click" />

 

 <asp:GridView ID="newsView" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="id_numeric,order_num"
                        OnRowCommand="newsView_RowCommand" OnRowDataBound="newsView_RowDataBound" OnRowDeleting="newsView_RowDeleting"
                        BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px"
                        CellPadding="4" OnPageIndexChanged="newsView_PageIndexChanged"
                        OnPageIndexChanging="newsView_PageIndexChanging" PageSize="12">
                        <Columns>
                        <asp:TemplateField HeaderText="">
                                <ItemTemplate>                                    
                                    <asp:CheckBox ID="chbFlag" runat="server" Checked='<%# Eval("DESCRIPTION").ToString()==""?false:true %>' />
                                    </a>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" Width="15px" />
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="样品ID">
                                <ItemTemplate>
                                    <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID_NUMERIC").ToString()%>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" />
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="分析方法ID">
                                <ItemTemplate>
                                  <asp:Label ID="lblANALYSIS_ID" runat="server" Text='<%# Eval("ANALYSIS_ID").ToString()%>'></asp:Label>  
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" />
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="组分ID">
                                <ItemTemplate>
                                  <asp:Label ID="lblCOMPONENT_NAME" runat="server" Text='<%# Eval("COMPONENT_NAME").ToString()%>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" />
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="分析项目">
                                <ItemTemplate>
                                   <asp:Label ID="lblNAME_PRN" runat="server" Text='<%# Eval("NAME_PRN").ToString()%>'></asp:Label>                                   
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" />
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="质量指标">
                                <ItemTemplate>
                                   <asp:Label ID="lblDESCRIPTION" runat="server" Text='<%# Eval("DESCRIPTION").ToString()%>'></asp:Label> 
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" />
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="实测值">
                                <ItemTemplate>                                   
                                   <asp:Label ID="lblTEXT" runat="server" Text='<%# Eval("TEXT").ToString()%>'></asp:Label>    
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" />
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="实验方法">
                                <ItemTemplate>
                                   <asp:Label ID="lblANALYSIS_PRN" runat="server" Text='<%# Eval("ANALYSIS_PRN").ToString()%>'></asp:Label> 
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Left" />
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="操作">
                                <ItemTemplate>
                                    <input id="iptOrderNum"  runat="server" value='<%# Eval("ORDER_NUM").ToString() %>' type="hidden" />
                                     
                                    <a href="javascript:void(0);" οnclick="javascript:thisWindowOpen('EditCert.aspx?ID_NUMERIC=<%# Eval("ID_NUMERIC").ToString().Trim() %>&ORDERNUM=<%# Eval("ORDER_NUM") %>');" style="cursor: hand" >
                                            编辑</a>   
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" />
                                <HeaderStyle HorizontalAlign="Center" />
                            </asp:TemplateField>
                        </Columns>
                        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                        <RowStyle BackColor="White" ForeColor="#003399" />
                        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                        <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#083898" Font-Bold="True" ForeColor="#CCCCFF" />
                    </asp:GridView>


 

后台  CS文件:

 

    /// <summary>
    /// 保存按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
  //获得GRIDVIEW数据
        DataTable dt = new DataTable();
        dt = getDataTable();
//对获得的DT数据进行保存到数据库中
    for (int i = 0; i < dt.Rows.Count; i++)
        {
                        sSql = @"update vgsm.web_coa
                       set analysis_id = '" + dt.Rows[i]["ANALYSIS_ID"].ToString() + @"',
                           analysis_prn = '" + dt.Rows[i]["ANALYSIS_PRN"].ToString() + @"',
                           component_name = '" + dt.Rows[i]["COMPONENT_NAME"].ToString() + @"',
                           name_prn = '" + dt.Rows[i]["NAME_PRN"].ToString() + @"',                          
                           description = '" + dt.Rows[i]["DESCRIPTION"].ToString() + @"',
                           text = '" + dt.Rows[i]["TEXT"].ToString() + @"',                           
                           isflag = '" + dt.Rows[i]["ISFLAG"].ToString() + @"'
                     where  id_numeric = lpad('" + dt.Rows[i]["ID_NUMERIC"].ToString() + "',10,' ') and order_num=" + dt.Rows[i]["ORDER_NUM"].ToString();
            Mydata.dosql(sSql);
        }
}



 

public DataTable getDataTable()
    {

        DataTable dt = new DataTable("inserttable");
        #region 
        DataColumn dcFlag = new DataColumn();
        dcFlag.DataType = System.Type.GetType("System.String");
        dcFlag.ColumnName = "ISFLAG";
        dt.Columns.Add(dcFlag);

        DataColumn dcID = new DataColumn();
        dcID.DataType = System.Type.GetType("System.String");
        dcID.ColumnName = "ID_NUMERIC";
        dt.Columns.Add(dcID);

        DataColumn dcAnaly = new DataColumn();
        dcAnaly.DataType = System.Type.GetType("System.String");
        dcAnaly.ColumnName = "ANALYSIS_ID";
        dt.Columns.Add(dcAnaly);

        DataColumn dcComponent = new DataColumn();
        dcComponent.DataType = System.Type.GetType("System.String");
        dcComponent.ColumnName = "COMPONENT_NAME";
        dt.Columns.Add(dcComponent);

        DataColumn dcNameprn = new DataColumn();
        dcNameprn.DataType = System.Type.GetType("System.String");
        dcNameprn.ColumnName = "NAME_PRN";
        dt.Columns.Add(dcNameprn);

        DataColumn dcDescription = new DataColumn();
        dcDescription.DataType = System.Type.GetType("System.String");
        dcDescription.ColumnName = "DESCRIPTION";
        dt.Columns.Add(dcDescription);

        DataColumn dcText = new DataColumn();
        dcText.DataType = System.Type.GetType("System.String");
        dcText.ColumnName = "TEXT";
        dt.Columns.Add(dcText);

        DataColumn dcAnalysisprn = new DataColumn();
        dcAnalysisprn.DataType = System.Type.GetType("System.String");
        dcAnalysisprn.ColumnName = "ANALYSIS_PRN";
        dt.Columns.Add(dcAnalysisprn);

        DataColumn dcOrdernum = new DataColumn();
        dcOrdernum.DataType = System.Type.GetType("System.String");
        dcOrdernum.ColumnName = "ORDER_NUM";
        dt.Columns.Add(dcOrdernum);

        //DataColumn dcshuliang = new DataColumn();
        //dcshuliang.DataType = System.Type.GetType("System.Int32");
        //dcshuliang.ColumnName = "shuliang";
        //dt.Columns.Add(dcshuliang);

        //DataColumn price = new DataColumn();
        //price.ColumnName = "price";
        //price.DataType = System.Type.GetType("System.Decimal");
        //dt.Columns.Add(price);
        #endregion
        
        string sTemp = string.Empty;
        for (int i = 0; i < newsView.Rows.Count; i++)
        {
            DataRow dr = dt.NewRow();
            CheckBox chbFlag = (CheckBox)newsView.Rows[i].Cells[0].FindControl("chbFlag");
            if (chbFlag.Checked == true)
            {
                dr["ISFLAG"] = "1";
            }
            else
            {
                dr["ISFLAG"] = "";
            }
            Label lblID  =(Label)newsView.Rows[i].Cells[1].FindControl("lblID");
            dr["ID_NUMERIC"] = lblID.Text;

            Label lblANALYSIS_ID = (Label)newsView.Rows[i].Cells[1].FindControl("lblANALYSIS_ID");
            dr["ANALYSIS_ID"] = lblANALYSIS_ID.Text;

            Label lblCOMPONENT_NAME = (Label)newsView.Rows[i].Cells[1].FindControl("lblCOMPONENT_NAME");
            dr["COMPONENT_NAME"] = lblCOMPONENT_NAME.Text;

            Label lblNAME_PRN = (Label)newsView.Rows[i].Cells[1].FindControl("lblNAME_PRN");
            dr["NAME_PRN"] = lblNAME_PRN.Text;

            Label lblDESCRIPTION = (Label)newsView.Rows[i].Cells[1].FindControl("lblDESCRIPTION");
            dr["DESCRIPTION"] = lblDESCRIPTION.Text;

            Label lblTEXT = (Label)newsView.Rows[i].Cells[1].FindControl("lblTEXT");
            dr["TEXT"] = lblTEXT.Text;

            Label lblANALYSIS_PRN = (Label)newsView.Rows[i].Cells[1].FindControl("lblANALYSIS_PRN");
            dr["ANALYSIS_PRN"] = lblANALYSIS_PRN.Text;

            HtmlInputHidden iptOrderNum = (HtmlInputHidden)newsView.Rows[i].Cells[0].FindControl("iptOrderNum");
            dr["ORDER_NUM"] = iptOrderNum.Value;

           // html = newsView.Rows[i].Cells[8].FindControl("aLink");
            dt.Rows.Add(dr);
            #region 
            //HtmlInputText t1 = (HtmlInputText)newsView.Rows[i].Cells[0].FindControl("chbFlag");
            //HtmlInputText t2 = (HtmlInputText)newsView.Rows[i].Cells[0].FindControl("TextBox2");

            TextBox t1 = (TextBox)GridView1.Rows[i].Cells[0].FindControl("TextBox1");
            TextBox t2 = (TextBox)GridView1.Rows[i].Cells[1].FindControl("TextBox2");
            //TextBox t3 = (TextBox)GridView1.Rows[i].Cells[2].FindControl("TextBox3");
            //TextBox t1 = (TextBox)GridView1.FindControl("TextBox1").Controls[0];
            //TextBox t2 = (TextBox)GridView1.FindControl("TextBox2").Controls[0];
            //TextBox t3 = (TextBox)GridView1.FindControl("TextBox3").Controls[0];
            //if (t1.Value != "")
            //{
            //    dr["name"] = t1.Value;//((TextBox)GridView1.FindControl("TextBox1").Controls[0]).Text;
            //    dr["shuliang"] = t2.Value; //((TextBox)GridView1.FindControl("TextBox2").Controls[0]).Text;
            //    dr["price"] = t3.Text;//((TextBox)GridView1.FindControl("TextBox3").Controls[0]).Text;
            //    dt.Rows.Add(dr);
            //}
            #endregion
        }
        //newsView.DataSource = dt;
        //newsView.DataBind();
        return dt;

    }

 

 

PS:在GRIDVIEW里面直接绑定数据时,在后台取各数据单格的数据(newsView.Rows[i].Cells[1].Text="")为空,所以为了方便,在里面加上Lable控件可以方便获取到其值!!!!

本人认为,此类取单元格的获取数据为空的情况有可能是因为前台的绑定数据机制、或是.NET程序绑定数据的顺序,有关。

取不到值的情况,还有待进一步确定验证。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值