【问题标题】:how to fire click event on cells of daragridview table in c#如何在c#中触发datagridview表单元格的点击事件
【发布时间】:2014-02-22 12:24:51
【问题描述】:

事件处理程序代码。

    private void dataGridView2_Click(object sender, DataGridViewCellEventArgs e)
    {
            DataGridViewImageCell cell = (DataGridViewImageCell)
            dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];
            if (cell.Value=="sewagram express")
            {
                SqlCommand cm1 = new SqlCommand("select * from sewagram", con);
                DataTable dth = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cm1);
                da.Fill(dth);
                dataGridView1.DataSource = dth;
            }
    }

【问题讨论】:

  • 我希望在单击单元格时可以使用此代码...请帮助
  • 变量cell 是ImageCell,但是,您将cell.Valuestring 进行比较。这很矛盾。 (ImageCell 将 Image 对象作为值)。
  • 先生,我该怎么办?
  • 那么我应该写什么代码......?

标签: c# winforms visual-studio-2010 datagridview event-handling


【解决方案1】:

sewagram express 可能没有价值。那是列标题文本或列名称。

您需要更改以下部分代码:

DataGridViewImageCell cell = (DataGridViewImageCell) dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (cell.Value=="sewagram express")
{

如果“sewagram express”是标题文本,你改变如下:

//     DataGridViewImageCell cell = 
//          (DataGridViewImageCell) dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (dataGridView1.Columns[e.ColumnIndex].HeaderText=="sewagram express")
{

如果“sewagram express”是列名,您更改如下:

//     DataGridViewImageCell cell = 
//          (DataGridViewImageCell) dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (((DataTable)(dataGridView1.DataSource)).Columns[e.ColumnIndex].ColumnName
         =="sewagram express")
{

你应该做的属于“sewagram express”。


已编辑

您使用DataAdapter.Fill() 为 DataGridView 创建 DataTable,因此您可以从 DataTable(datagridview 的数据源)的 TableName 属性中获取表名。

// DataTable dtSource = (DataTable)dataGridView1.DataSource;
// sorry, you need check the tablename of datagridview2's datasource. I fixed.
DataTable dtSource = (DataTable)dataGridView2.DataSource;
if (dtSource.TableName == "sewagram express")
{

已编辑

我之前的帖子有错别字,已修改。

您需要检查 TableName dataGridView2 的值(换句话说,单击 DataGridView )。
我再次编写整个代码并附加行以显示 MessageBox 以检查 TableName 值。
MessageBox 中的显示值是预期值吗?还是不行?

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
    DataTable dtSource = (DataTable)dataGridView2.DataSource;
    MessageBox.Show(dtSource.TableName,"");  // check the value of TableName

    if (dtSource.TableName == "sewagram express")
    {
        SqlCommand cm1 = new SqlCommand("select * from sewagram", con);
        DataTable dth = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cm1);
        da.Fill(dth);
        dataGridView1.DataSource = dth;
    }
}

【讨论】:

  • sewagram express 是表中的一个项目..它也是一个表名..所以当我单击单元格时,它应该在其他 datagridview 中显示名为 sewagram express 的表.. .那个代码写在if语句中。
  • if ((DataTable)(dataGridView1.DataSource).Columns[e.ColumnIndex].ColumnName =="sewagram express")
  • 我明白了,“sewagram”和“sewagram express”都是表名。
  • 抱歉,Columns[e.ColumnIndex] 有误。我已经修好了。我很快就会附加代码来检查表名。
  • 先生,现在如何处理 Columns[e.ColumnIndex]?
【解决方案2】:

您必须附加CellClick 事件。

dataGridView2.CellClick += dataGridView2_CellClick;

然后定义事件处理器如下:

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
    DataGridViewImageCell cell = (DataGridViewImageCell) dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];
    if (cell.Value=="sewagram express")
    {
        SqlCommand cm1 = new SqlCommand("select * from sewagram", con);
        DataTable dth = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cm1);
        da.Fill(dth);
        dataGridView1.DataSource = dth;
    }
}

【讨论】:

  • 先生仍然无法正常工作....当我单击名为“sewagram express”的单元格时,它没有触发代码。
  • 亲爱的先生,为了您的好意,该牢房应该被解雇。您可以通过在if 行上设置breakpoint 来检查它,您正在尝试检查单元格值是否等于sewagram express。但这总是会失败,因为 Image 字段没有 string 值。所以你需要相应地改变你的条件。顺便说一句,另一个答案已经被接受。祝你好运!非常感谢。
  • 先生,sewagram express 是一个字符串......无论如何我希望它应该被点击并且必须触发一些代码......它没有发生任何事情......
  • 不,先生,代码中没有错误,但它不起作用
猜你喜欢
  • 2012-09-27
  • 2015-09-25
  • 2021-02-07
  • 2014-12-30
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 2014-12-28
相关资源
最近更新 更多