English 中文(简体)
如何将GridView行添加到DataTable中?
原标题:How to add gridview rows to a datatable?

我有一个GridView,它将包含一些n行。现在我想将GridView的所有行添加到一个数据表中,该数据表将用于批量复制操作。

我发现了这个链接:http://www.codeproject.com/KB/aspnet/GridView_To_DataTable.aspx

But i want all columns of my gridview to be added to the datarow of the datatable Grid http://img85.imageshack.us/img85/4044/gridp.jpg

我想在提交时将GridView转换为DataTable.... 有什么建议...

编辑:

回答如下:工程,一找到答案......

    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("EmpId", typeof(Int64)));
    dt.Columns.Add(new DataColumn("FromDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("ToDate", typeof(DateTime)));
    dt.Columns.Add(new DataColumn("DaysPresent", typeof(double)));
    dt.Columns.Add(new DataColumn("OpeningAdvance", typeof(double)));
    dt.Columns.Add(new DataColumn("AdvanceDeducted", typeof(double)));
    dt.Columns.Add(new DataColumn("RemainingAdvance", typeof(double)));
    dt.Columns.Add(new DataColumn("SalaryGiven", typeof(double)));
    dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));

    foreach (GridViewRow row in gridEmployee.Rows) 
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            DataRow dr = dt.NewRow();
            dr["EmpId"] = Convert.ToInt64(((HiddenField)row.Cells[0].FindControl("HiddenId")).Value);
            dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) +  /  + fromdate[0].ToString() +  /  + fromdate[2].ToString());
            dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) +  /  + todate[0].ToString() +  /  + todate[2].ToString());
            dr["DaysPresent"] = Convert.ToDouble(((TextBox)row.Cells[3].FindControl("TxtDaysPresent")).Text);
            dr["OpeningAdvance"] = Convert.ToDouble(((TextBox)row.Cells[4].FindControl("txtOpeningAdv")).Text);
            dr["AdvanceDeducted"] = Convert.ToDouble(((TextBox)row.Cells[5].FindControl("TxtAdvanceDeducted")).Text);
            dr["RemainingAdvance"] = Convert.ToDouble(((TextBox)row.Cells[6].FindControl("TxtClosingAdvance")).Text);
            dr["SalaryGiven"] = Convert.ToDouble(((TextBox)row.Cells[7].FindControl("TxtSalary")).Text);
            dr["CreatedDate"] = Convert.ToDateTime(System.DateTime.Now.ToString());
            dt.Rows.Add(dr);
        }
    }
    SqlBulkCopy sbc = new SqlBulkCopy(connectionString);
    sbc.DestinationTableName = "SalaryDetails";
    sbc.ColumnMappings.Add("EmpId", "EmpId");
    sbc.ColumnMappings.Add("FromDate", "FromDate");
    sbc.ColumnMappings.Add("ToDate", "ToDate");
    sbc.ColumnMappings.Add("DaysPresent", "DaysPresent");
    sbc.ColumnMappings.Add("OpeningAdvance", "OpeningAdvance");
    sbc.ColumnMappings.Add("AdvanceDeducted", "AdvanceDeducted");
    sbc.ColumnMappings.Add("RemainingAdvance", "RemainingAdvance");
    sbc.ColumnMappings.Add("SalaryGiven", "SalaryGiven");
    sbc.ColumnMappings.Add("CreatedDate", "CreatedDate");
    sbc.WriteToServer(dt);
    sbc.Close();
最佳回答

你可以逐行遍历数据网格,并制作逗号分隔文件。然后使用Bulk insert或bcp将数据插入数据库。

另一个解决方案 (lìng yī gè jiě jué fāng àn)

    DataTable dt = new DataTable();    
    for (int j = 0; j < grdList.Rows.Count; j++)
    {
        DataRow dr;
        GridViewRow row = grdList.Rows[j];
        dr = dt.NewRow();
        for (int i = 0; i < row.Cells.Count; i++)
        {
            dr[i] = row.Cells[i].Text;
        }

        dt.Rows.Add(dr);
    }

SqlBulkCopy sbc = new SqlBulkCopy(targetConnStr);
sbc.DestinationTableName = "yourDestinationTable";
sbc.WriteToServer(dt);
sbc.Close();
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签