Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
663 views
in Technique[技术] by (71.8m points)

c# - How to add gridview rows to a datatable?

I have a gridview which will contain some 'n' number of rows.... Now i want to add all rows of the gridview to a datatable which will be used for bulkcopy operation...

I have found this 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

I want to convert gridview to datatable on submit.... Any suggestion...

EDIT:

Answer below works and i have found an answer too...

    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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can traverse datagrid row by row and make a comma separated file. then use Bulk insert or bcp for inserting data to db.

Another Solution

    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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...