将TreeView节点拖到DataGridView单元格问题

时间:2009-12-06 15:04:06

标签: c# datagridview drag-and-drop

我已编写此例程来拖动TreeView节点值以将其放在DataGridView单元格上。

private void dataGridView1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(string)))
            {
                #region Find Row and Cell from Mouse Position
                Point grvScreenLocation = dataGridView1.PointToScreen(dataGridView1.Location);
                int tempX = DataGridView.MousePosition.X - grvScreenLocation.X + dataGridView1.Left;
                int tempY = DataGridView.MousePosition.Y - grvScreenLocation.Y + dataGridView1.Top;

                DataGridView.HitTestInfo hit = dataGridView1.HitTest(tempX, tempY);

                int cellX = hit.RowIndex;
                int cellY = hit.ColumnIndex;
                #endregion

                string droppedString = e.Data.GetData(typeof(string)) as string;

                DataGridViewRow selectedRow = dataGridView1.Rows[cellX];
                DataGridViewCell selectedCell = dataGridView1.Rows[cellX].Cells[cellY];

                if (!selectedRow.IsNewRow)
                {
                    selectedCell.Value = droppedString;
                    selectedCell.Tag = droppedString;
                    selectedCell.OwningRow.Cells[3].Value = colMappingType.Items[0];
                }
                else
                {                    
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[dataGridView1.Rows.Count-2].Cells[cellY].Value = droppedString;
                    dataGridView1.Rows[dataGridView1.Rows.Count-2].Cells[cellY].Tag = droppedString;
                    dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[3].Value = colMappingType.Items[1];
                }
            }
        }

我已编写此例程来测试是否实际填充了单元格:

private void button1_Click(object sender, EventArgs e)
        {
            DataTable dataTable = new DataTable();
            dataTable.TableName = "Test";

            dataTable.Columns.Add("ColOne", typeof(bool));
            dataTable.Columns.Add("ColTwo", typeof(string));
            dataTable.Columns.Add("ColThr", typeof(string));
            dataTable.Columns.Add("ColFor");

            foreach (DataGridViewRow dgvRow in dataGridView1.Rows)
            {
                DataRow dataRow = dataTable.NewRow();

                int i=0;
                foreach (DataGridViewCell dgvCell in dgvRow.Cells)
                {
                    dataRow.ItemArray.SetValue(dgvCell.Value, i);
                    ++i;
                }

                dataTable.Rows.Add(dataRow);                
            }

            Form2 f = new Form2();
            f.DataGridView.DataSource = dataTable;
            f.Show();
        }
    }

测试代码显示DataGridView上的新Form2具有正确的行数和列数。但不包含任何数据。

实际发生了什么?

如何解决这个问题?

编辑:

此代码解决了这个问题。

private void button1_Click(object sender, EventArgs e)
        {
            DataTable dataTable = new DataTable();
            dataTable.TableName = "Test";

            dataTable.Columns.Add("ColOne", typeof(bool));
            dataTable.Columns.Add("ColTwo", typeof(string));
            dataTable.Columns.Add("ColThr", typeof(string));
            dataTable.Columns.Add("ColFor");

            foreach (DataGridViewRow dgvRow in dataGridView1.Rows)
            {
                if (dgvRow.Cells[0].Value != null
                    && dgvRow.Cells[1].Value != null
                    && dgvRow.Cells[2].Value != null
                    && dgvRow.Cells[3].Value != null)
                {
                    DataRow dataRow = dataTable.NewRow();

                    dataRow[0] = dgvRow.Cells[0].Value;
                    dataRow[1] = dgvRow.Cells[1].Value;
                    dataRow[2] = dgvRow.Cells[2].Value;
                    dataRow[3] = dgvRow.Cells[3].Value;

                    dataTable.Rows.Add(dataRow);
                }
            }

            Form2 f = new Form2();
            f.DataGridView.DataSource = dataTable;
            f.Show();
        }

1 个答案:

答案 0 :(得分:1)

我甚至不确定如何正确使用ItemArray,但你现在的方式不起作用。替换:

 dataRow.ItemArray.SetValue(dgvCell.Value, i);

dataRow[i] = dgvCell.Value;