2010-02-25 54 views
4

我有一個gridview,它將包含一些'n'行數....現在我想要將gridview的所有行添加到將用於批量拷貝操作的數據表中...如何將gridview行添加到數據表?

我已經找到這http://www.codeproject.com/KB/aspnet/GridView_To_DataTable.aspx

但我想我的GridView的所有列添加到DataTable中 Grid http://img85.imageshack.us/img85/4044/gridp.jpg

我想轉換GridView控件的數據表上提交....任何建議的數據行...

編輯:

回答以下的作品,我已經找到了答案太...

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

你是如何在第一個地方的數據?從DataTable? – madatanic 2010-02-25 06:29:31

+0

@madatanic但我會得到只有標籤數據... – 2010-02-25 06:31:48

回答

2

您可以按行遍歷DataGrid行,並用逗號分隔的文件。然後使用批量插入或bcp將數據插入到數據庫。

另一種解決方案

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

@masoud任何樣本.. – 2010-02-25 06:33:40

+0

@Pandiya:請參閱編輯的答案。 – 2010-02-25 06:51:34

+0

@masoud foreach語句不能對'System.Web.UI.WebControls.GridView'類型的變量進行操作,因爲'System.Web.UI.WebControls.GridView'不包含'GetEnumerator'的公共定義 – 2010-02-25 06:52:47