从Windows窗体应用程序

时间:2016-03-30 20:28:22

标签: c# .net winforms datagridview

我正在使用可编辑的DataGridView,我希望在编辑DataGridView中的行后获取该行。

这是我的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);
    }

我想要的是:

我应该能够DataRow动态输入DataGridView

3 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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);
                        }

                    }
            }

这很简单。

答案 2 :(得分:0)

如果你需要返回List

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;
}