我想使用linq或将gridview数据复制到数据表,将数据从网格视图保存到数据库 [英] I want to save data from grid view to database using linq or copy gridview data to datatable

查看:58
本文介绍了我想使用linq或将gridview数据复制到数据表,将数据从网格视图保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void binddatatable()
   {
       DataTable dt = new DataTable("mydata");
       dt.Columns.Add("Quono", typeof(string));
       dt.Columns.Add("area", typeof(float));
       dt.Columns.Add("srno", typeof(int));
       dt.Columns.Add("product_Name", typeof(string));
       dt.Columns.Add("Quantity", typeof(float));
       dt.Columns.Add("Units", typeof(string));
       dt.Columns.Add("Rate", typeof(float));
       dt.Columns.Add("Amount", typeof(float));
       ViewState["mydata"] = dt;

       if (grdqutotation.Rows.Count != 0)
       {
           //Forloop for header
           dt.Columns.Add(grdqutotation.HeaderRow.Cells[i].Text);
           for (int i = 0; i < grdqutotation.HeaderRow.Cells.Count; i++)
           {

           }
           //foreach for datarow
           foreach (GridViewRow row in grdqutotation.Rows)
           {
               DataRow dr = dt.NewRow();
               for (int j = 0; j < row.Cells.Count; j++)
               {
                   dr[grdqutotation.HeaderRow.Cells[j].Text] = row.Cells[j].Text;
               }
               dt.Rows.Add(dr);
           }
           //Loop for footer
           if (grdqutotation.FooterRow.Cells.Count != 0)
           {
               DataRow dr = dt.NewRow();
               for (int i = 0; i < grdqutotation.FooterRow.Cells.Count; i++)
               {
                   //You have to re-do the work if you did anything in databound for footer.
               }
               dt.Rows.Add(dr);
           }
           dt.TableName = "Tbl_SubFinalQuotation";
       }
   }





我的尝试:



protected void binddatatable()

{

DataTable dt = new DataTable(mydata);

dt.Columns.Add(Quono,typeof(string));

dt.Columns.Add(area,typeof(float));

dt.Columns.Add(srno,typeof(int));

dt.Columns.Add(product_Name,typeof(string));

dt。 Columns.Add(Quantity,typeof(float));

dt.Columns.Add(Units,typeof(string));

dt.Columns。添加(Rate,typeof(float));

dt.Columns.Add(Amount,typeof(float));

ViewState [mydata] = dt;



if(grdqutotation.Rows.Count!= 0)

{

// Forloop for header

dt.Columns.Add(grdqutotation.HeaderRow.Cells [i] .Text);

for(int i = 0; i< grdqutota tion.HeaderRow.Cells.Count; i ++)

{



}

// foreach for datarow

foreach (grdqutotation.Rows中的GridViewRow行)

{

DataRow dr = dt.NewRow();

for(int j = 0; j < row.Cells.Count; j ++)

{

dr [grdqutotation.HeaderRow.Cells [j] .Text] = row.Cells [j] .Text;

}

dt.Rows.Add(dr);

}

//页脚循环

if(grdqutotation.FooterRow.Cells.Count!= 0)

{

DataRow dr = dt.NewRow();

for(int i = 0; i< grdqutotation.FooterRow.Cells.Count; i ++)

{

//你必须重新开始工作如果你在数据绑定中为页脚做了什么。

}

dt.Rows.Add(dr);

}

dt.TableName =Tbl_SubFinalQuotation ;

}

}



What I have tried:

protected void binddatatable()
{
DataTable dt = new DataTable("mydata");
dt.Columns.Add("Quono", typeof(string));
dt.Columns.Add("area", typeof(float));
dt.Columns.Add("srno", typeof(int));
dt.Columns.Add("product_Name", typeof(string));
dt.Columns.Add("Quantity", typeof(float));
dt.Columns.Add("Units", typeof(string));
dt.Columns.Add("Rate", typeof(float));
dt.Columns.Add("Amount", typeof(float));
ViewState["mydata"] = dt;

if (grdqutotation.Rows.Count != 0)
{
//Forloop for header
dt.Columns.Add(grdqutotation.HeaderRow.Cells[i].Text);
for (int i = 0; i < grdqutotation.HeaderRow.Cells.Count; i++)
{

}
//foreach for datarow
foreach (GridViewRow row in grdqutotation.Rows)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < row.Cells.Count; j++)
{
dr[grdqutotation.HeaderRow.Cells[j].Text] = row.Cells[j].Text;
}
dt.Rows.Add(dr);
}
//Loop for footer
if (grdqutotation.FooterRow.Cells.Count != 0)
{
DataRow dr = dt.NewRow();
for (int i = 0; i < grdqutotation.FooterRow.Cells.Count; i++)
{
//You have to re-do the work if you did anything in databound for footer.
}
dt.Rows.Add(dr);
}
dt.TableName = "Tbl_SubFinalQuotation";
}
}

推荐答案

好的,获取数据后只需在sql server中创建一个表类型从

应用程序传递数据表作为参数。
Ok,after getting datatable just create a table type in sql server and pass data table from
application as a parameter.


这篇关于我想使用linq或将gridview数据复制到数据表,将数据从网格视图保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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