【问题标题】:Can't get a value from DataGridView to another form无法从 DataGridView 获取值到另一个表单
【发布时间】:2013-11-11 20:48:16
【问题描述】:

我有一个form,它与主要的分开弹出。它上面有一个DataGridView,它显示了用户访问过的所有网址。所有的网址都保存在一个字符串列表中,然后转换为DataTable,然后我将值放入DataGridView。我可以看到带有网址的表格。

我想启用双击单元格,这样如果用户双击它,他将被转换为返回该 URL。

选择一行(单击 url)并将其解析为字符串时出现错误。由于页面未加载,似乎它是空的

private DataTable ConvertListToDataTable(List<string> l)
    {
        table.Columns.Add("urls");
        int i = 0;
        foreach(string s in l)
        {
            table.Rows.Add();
            table.Rows[i].SetField("urls", s);
            i++;
        }
        return table;
    }
public void pageload(List<string> list)
    {
        table = ConvertListToDataTable(list);
        dataGridView1.DataSource = table;
    }

    private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        Form1 form = new Form1();
        form.pageInput.Text = dataGridView1.SelectedRows.ToString();
        string input = form.pageInput.Text;
        form.loadPage(input);
    }

我尝试将 DataGridView SelectionMode 更改为 FullRowSelectCellSelectRowHeaderSelect,但没有任何改变。试图更改单元格和行的双击。我还尝试在表单上添加textBox 以进行调试。所以当我分配时:

textBox1.Text = dataGridView1.SelectedRows.ToString()

我得到的只是Windows.Forms.DataGridViewSelectedCellCollection,所以它肯定是空的。

问题是,如果ToString() 不起作用,我该如何取回该 url 值?谢谢您的帮助。

【问题讨论】:

  • 不是CellContentDoubleClick会在edit mode中设置double-clicked cell吗?
  • 您遇到的错误是什么?
  • @King 不知道你在说什么,但目前的编辑模式是 EditOnKeyStrokeOrF2。我应该将其更改为 EditProgrammatically 吗?
  • @Messerschmitt 我可以使用double-click 将当前单元格设置为编辑模式,这是正常行为。虽然它可能不像double-click,但即使您第一次单击单元格,然后再次单击它,它将被设置为编辑模式。
  • @Brian,消息说:UriFormatException 未处理。无效的 URI:无法确定 URI 的格式。当我调用 dataGridView.SelectedRows.ToString() 时,我可能会在 url 字符串为空时收到此错误

标签: c# winforms datagridview datatable


【解决方案1】:

如果它是一个集合,则必须单独处理集合中的每个元素,否则 .ToString() 只会在 DataGridViewSelectedCellCollection 元数据中返回 selectedRows 的文本。

试试类似的,

foreach(elementType i in dataGridView1.SelectedRows)
{
   //do something you want here such as TextBox.Text = TextBox.Text + i.ToString();
}

【讨论】:

  • 谢谢。我试图实现这一点,但现在我在文本框中显示了 DataGridViewRow {Index=0},并且在加载页面时仍然出现错误。好吧,因为它不是一个有效的 url。
  • dataGridView 是否包含 url?听起来没有包含任何 url 的单行
  • 是的,dataGridView 只是一个显示访问过的 url 的表格。好吧,至少这些网址显示在表格中,但是当我双击任何一行时,该行似乎没有任何内容。
  • 如果不是 selectedrows 则尝试抓取所有行并查看其中是否有任何 url,如果 gridview 中确实显示了 url
  • 试过了。它是空的:(
【解决方案2】:

首先,您想要的事件可能是CellDoubleClick() 而不是CellContentDoubleClick()

然后这样做:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    //Makes sure that a valid cell was double clicked.
    if (e.ColumnIndex > -1 && e.RowIndex > -1)
    {
        Form1 form = new Form1();
        form.pageInput.Text = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
        string input = form.pageInput.Text;
        form.loadPage(input);
    }
}

让我知道这是否有效!

【讨论】:

  • 不幸的是,它没有任何区别
【解决方案3】:

我认为你想要/需要的更像是这样的:

private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{      
    string URL = "";
    if (dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
    {  
        //get the URL from the cell that you double-clicked on
        URL = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
    }
    else
        return;

    //put your code to launch the URL in a browser here        
}

另一种选择是像这样使用 MouseDoubleClick 事件:

private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    //this will allow us to find out where we clicked in the datagridview
    DataGridView.HitTestInfo hti = dataGridView1.HitTest(e.X, e.Y);

    if (e.Button == MouseButtons.Left && hti.Type == DataGridViewHitTestType.Cell)
    {
        string URL = "";
        if (dataGridView1[hti.ColumnIndex, hti.RowIndex].Value != null)
        {
            //get the URL from the cell that you double-clicked on
            URL = dataGridView1[hti.ColumnIndex, hti.RowIndex].Value.ToString();
        }
        else

            return;
        //put your code to launch the URL in a browser here 
    }
}

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    相关资源
    最近更新 更多