【问题标题】:Split values using drag and drop使用拖放拆分值
【发布时间】:2014-10-27 13:26:24
【问题描述】:
textbox1.text = 123 ;
textbox2.text = examples ;

我在listbox1中的两个文本框值上方添加了列表框

listbox = 123 examples添加like show后

    private void dataGridView1_DragDrop(object sender, DragEventArgs e)
    {
        Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
        DataGridView.HitTestInfo hti = dataGridView1.HitTest(clientPoint.X, clientPoint.Y);
        DataGridViewCell targetCell = dataGridView1[hti.ColumnIndex, hti.RowIndex];

        if (targetCell.Value == null)
        {
            targetCell.Value = e.Data.GetData(DataFormats.Text);
        }
        else
        {
            if (targetCell.Value.ToString().Contains(e.Data.GetData(DataFormats.Text).ToString().Split(' ')[1]))
            {

            }
            else
            {
                targetCell.Value = targetCell.Value.ToString().Split(' ')[1] + System.Environment.NewLine + e.Data.GetData(DataFormats.Text);

            }
        }

        textBox3.Text = e.Data.GetData("System.String").ToString().Split()[0];
    }

现在,如果我将列表框项(123 个示例)中的任何单元格拖放到我的 datagridview 中,它应该像 textbox3.text = 123;以及 datagridview 单元格的示例。

上面的代码使用但它的拆分但没有解决。

视觉示例:

【问题讨论】:

  • 将“文本”添加到文本框的方式不起作用。您需要将它们放在引号中,例如textbox2.text = "examples";
  • 是的,我已经加了引号,但是拖放后给我们带来了问题。
  • 你的代码的输出是什么?
  • 输出是“123”在textbox3中,“123 sometext”在datagridview单元格中,表示123在两者中重复。

标签: c# datagridview drag-and-drop


【解决方案1】:

问题是,你的逻辑是错误的。

只需替换这段代码sn-p

if (targetCell.Value == null)
{
    targetCell.Value = e.Data.GetData(DataFormats.Text);
}
else
{
    if (targetCell.Value.ToString().Contains(e.Data.GetData(DataFormats.Text).ToString().Split(' ')[1]))
    {

    }
    else
    {
        targetCell.Value = targetCell.Value.ToString().Split(' ')[1] + System.Environment.NewLine + e.Data.GetData(DataFormats.Text);

    }
}

用这一行:

targetCell.Value = e.Data.GetData(DataFormats.Text).ToString().Split(' ')[1];

对于“文本框”的动态选择:

创建一个由 TextBoxes 组成的全局数组并在表单的构造函数中对其进行初始化:

TextBox[] arrTextBox;

public Form1()
{
    InitializeComponent();

    //initializing the array
    arrTextBox = new TextBox[] { textBox1, textBox2, textBox3, textBox4 };
}

现在像这样使用它:

//get RowIndex of selected Cell and get the same index TextBox from the array
arrTextBox[targetCell.RowIndex].Text = e.Data.GetData("System.String").ToString().Split()[0];

完整代码:

TextBox[] arrTextBox;

public Form1()
{
    InitializeComponent();

    //initializing the array
    arrTextBox = new TextBox[] { textBox1, textBox2, textBox3, textBox4 };
}

private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
    Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
    DataGridView.HitTestInfo hti = dataGridView1.HitTest(clientPoint.X, clientPoint.Y);
    DataGridViewCell targetCell = dataGridView1[hti.ColumnIndex, hti.RowIndex];

    targetCell.Value = e.Data.GetData(DataFormats.Text).ToString().Split(' ')[1];   //for GridCell

    arrTextBox[targetCell.RowIndex].Text = e.Data.GetData("System.String").ToString().Split()[0];   //for TextBox
}

【讨论】:

  • 现在结果如何?
  • 同上,123在textbox3中,123在datagrid视图单元格中
  • 单元格的初始值是多少(在删除 listItem 之前)?
  • 你的场景对我来说仍然不是很清楚。你能展示一个直观的例子吗?
  • 如何添加我的可视文件。无法上传 rar 文件。建议我的步骤。
猜你喜欢
  • 1970-01-01
  • 2016-11-12
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
  • 1970-01-01
  • 2012-07-12
  • 1970-01-01
相关资源
最近更新 更多