如何在Datagridview项目上单击事件?...如果我点击任何项目它必须触发代码 [英] How To Fire Click Event On Datagridview Items?...If I Click On Any Item It Must Fire A Code

查看:86
本文介绍了如何在Datagridview项目上单击事件?...如果我点击任何项目它必须触发代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //DataGridViewImageCell cell = (DataGridViewImageCell)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];
            //if (dataGridView1.Columns[e.RowIndex].Name == "Sewagram express")

            //if ((DataTable)(dataGridView2.DataSource).Columns[e.RowIndex].RowName
              //       == "sewagram express")
            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;
            }
        }

推荐答案

当你引用dataGridView 2时,这有点令人困惑在dataGridView 1 中单击的行和列号。所以假设你真的打算这样做(并且两个网格都包含相同的文字sewagram express......



在你的点击事件中(通过它的声音)你已声明 DataGridView 图像单元格但你想将它与一个字符串进行比较...这听起来不对对我来说所以我改为
This is a little confusing as you are referencing dataGridView2 with the row and column numbers of where you have clicked in dataGridView1. So assuming you really meant to do that (and that both grids contain the same text "sewagram express"...

In your click event (which is firing ok by the sound of it) you have declared DataGridViewImageCell cell but you want to compare this to a string ... this doesn't sound right to me so I changed that to
DataGridViewCell cell = (DataGridViewCell)dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];



我遇到的下一个问题是


The next problem I hit is with

if (cell.Value=="sewagram express")<br />

它会抛出警告

Quote:

警告1可能的非预期参考比较;要获得值比较,请将左侧投射到类型'string'C:\ Users \ C_2 \ AppData \ Local \Temporary Projects \ WindowsFormsApplication1 \Form1.cs 34 17 WindowsFormsApplication1

Warning 1 Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string' C:\Users\C_2\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 34 17 WindowsFormsApplication1

所以我将其更改为

if (cell.Value.ToString() == "sewagram express")

,代码按预期运行。最后一步并非绝对必要,因为如果输入? cell.Value.GetType()进入立即窗口,你会发现它实际上是一个字符串,但我真的不喜欢在我的代码中留下警告。



我通过在事件中放置一个断点并通过它仔细记录我的错误,输出和本地窗口中出现的内容来找到所有这些。

and the code ran as expected. This last step wasn't strictly necessary because if you type ? cell.Value.GetType() into the Immediate window you'll find that it is actually a string, but I really don't like leaving Warnings in my code.

I found all of this out by putting a breakpoint in the event and stepped through it taking careful note of what was appearing in my Errors, Output and Locals windows.


这篇关于如何在Datagridview项目上单击事件?...如果我点击任何项目它必须触发代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆