2016-03-30 28 views
0

我正在使用可編輯DataGridView,我想在編輯DataGridView中的行後獲取該行。從Windows窗體應用程序中的可編輯DataGridView中獲取DataRow

這裏是我的DataGridView截圖:

enter image description here

我的問題是:

我可以輸入任意多行,我可以。點擊「註冊」按鈕,我無法從DataGridView獲得行。

我想的是:

private void enrollmentRegisterButton_Click(object sender, EventArgs e) 
    { 
     DataTable data = (DataTable)(childrenDetailsDataGridView.DataSource); 
     DataRow[] drArray = new DataRow[data.Rows.Count]; 
     data.Rows.CopyTo(drArray, 0); 
    } 

我要的是:

我應該能夠得到DataRowDataGridView動態輸入。

+0

你設置'DataSource'電網的'DataTable'?你的意思是你需要在加載數據後只添加新的行嗎? –

+0

我找到了解決方案 – Vikash

+0

** - **爲什麼您將'Child'的所有屬性定義爲字符串? ** - **「兒童」班的定義在哪裏? ** - **目前的答案有一些問題。還有更好的方法來獲取值並填寫打印列表。 ** - **如果您想獲得更好的答案,請編輯您的問題。現在問題和答案是不相關的。 –

回答

0

我找到了解決辦法:

List<Child> childrenList = new List<Child>(); 


      foreach (DataGridViewRow row in childrenDetailsDataGridView.Rows) 
      { 
       bool IsRowEmpty = true; 
        if (row.Cells.Count > 0) 
        { 
         Child child = new Child(); 
         if (row.Cells["Childname"].Value !=null) 
         { 
          IsRowEmpty = false; 
          child.Childname = row.Cells["Childname"].Value.ToString(); 
         } 

         if (row.Cells["Gender"].Value != null) 
         { 
          child.Gender = row.Cells["Gender"].Value.ToString(); 
         } 

         if (row.Cells["Age"].Value != null) 
         { 
          child.Age = row.Cells["Age"].Value.ToString(); 
         } 

         if (row.Cells["Star"].Value != null) 
         { 
          child.Star = row.Cells["Star"].Value.ToString(); 
         } 

         if (row.Cells["Occupation"].Value != null) 
         { 
          child.Occupation = row.Cells["Occupation"].Value.ToString(); 
         } 
         if (!IsRowEmpty) 
         { 
          childrenList.Add(child); 
         } 

        } 
      } 

這很簡單。

1

要從DataGridView獲取數據,請使您的代碼如此。 然後你可以從DataTable中獲取行。

private void button1_Click(object sender, EventArgs e) 
    { 
     // Getting data from DataGridView 
     DataTable myDt = new DataTable(); 
     myDt = GetDTfromDGV(dataGridView1); 
    } 
private DataTable GetDTfromDGV(DataGridView dgv) 
    { 
     // Macking our DataTable 
     DataTable dt = new DataTable(); 
     dt.Columns.AddRange(new DataColumn[6] 
      { 
       new DataColumn(dgv.Columns[0].Name, typeof(string)), 
       new DataColumn(dgv.Columns[1].Name, typeof(string)), 
       new DataColumn(dgv.Columns[2].Name, typeof(string)), 
       new DataColumn(dgv.Columns[3].Name, typeof(string)), 
       new DataColumn(dgv.Columns[4].Name, typeof(int)), 
       new DataColumn(dgv.Columns[5].Name, typeof(int)) 
      }); 
     // Getting data 
     foreach (DataGridViewRow dgvRow in dgv.Rows) 
     { 
      DataRow dr = dt.NewRow(); 
      for (int col = 0; col < dgv.Columns.Count; col++) 
      { 
       dr[col] = dgvRow.Cells[col].Value == null ? DBNull.Value : dgvRow.Cells[col].Value; 
      } 
      dt.Rows.Add(dr); 
     } 
     // removing empty rows 
     for (int row = dt.Rows.Count - 1; row >= 0; row--) 
     { 
      bool flag = true; 
      for (int col = 0; col < dt.Columns.Count; col++) 
      { 
       if (dt.Rows[row][col] != DBNull.Value) 
       { 
        flag = false; 
        break; 
       } 
      } 
      if (flag == true) 
      { 
       dt.Rows.RemoveAt(row); 
      } 
     } 
     return dt; 
    } 
0

如果你需要返回列表

private void button1_Click(object sender, EventArgs e) 
{ 
    // Getting data from DataGridView 
    var list = GetDTfromDGV(dataGridView1); 
} 

private dynamic GetDTfromDGV(DataGridView dgv) 
{ 
    ..... 
    ..... 

    var list = dt.AsEnumerable().ToList(); 

    return list; 
} 
相關問題