c# - 将行从 datagridview 拖放到 TextBox
问题描述
不传输文本。该代码不是我的,它最初与listBox
.
我可以移动单个单元格,但我需要在这里移动一行。DataGridview
在表单中选择数据源。
public Form1()
{
InitializeComponent();
textBox1.AllowDrop = true;
dataGridView1.AllowDrop = true;
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Move);
}
private void textBox1_DragEnter(object sender, DragEventArgs e){
if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
e.Effect = DragDropEffects.Move;
}
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));
foreach (DataGridViewRow row in rows)
{
textBox1.Text = row.Cell[1].Value.ToString();
}
}
解决方案
尝试添加和修改代码textBox1_DragDrop
如下。
foreach (DataGridViewRow row in rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
textBox1.Text += cell.Value.ToString() + " ";
}
}
测试结果,
更新
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
{
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
textBox1.Text += dataGridView1.Rows[cell.RowIndex].Cells[i].Value.ToString() + " ";
}
}
}
推荐阅读
- vue.js - Vue变量在组件内部未定义
- azure - Azure WebApp 和 SQLAzure 上的 Sonarqube 容器
- python - 为什么 wx.Panel 在这个简单的代码中搞砸了 Sizer?
- kubernetes - 自动轮换生成的随机密码
- php - 有没有更好的方法循环遍历这些 PHP 代码,也许是 foreach 循环?
- flutter - 是否可以使用 Flutter 在 Google Maps Autocomplete 上仅显示特定地点?
- r - A function to run correlations for several variables
- stream - 通过 HTML5 将网络摄像头流式传输到 RMTP 服务器?
- c++11 - 通过函数参数评估顺序将 ODBC 绑定映射到函数回调
- python - 如何在 Atom 编辑器中运行代码?