0

ShowRichMessageBox当我单击时,我想在一行中显示一个特定的单元格值,button但是如果单击该行上的任意位置,此事件将显示单元格值....!

这里有什么问题.....我该如何解决上述问题???

我有一些很大的对数值,但它已经加载到单元格中,所以, Is it possible to expand the row when i select a particular row in the datagridview???

 public LogView()
    {
        InitializeComponent();
        this.dataGridView2.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView2_buttonCol);

        bindingList = new SortedBindingList<ILogItemView>();
        dataGridView2.DataSource = bindingList;
        this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        this.dataGridView2.MultiSelect = false;
        var buttonCol = new DataGridViewButtonColumn(); // The button to display a particular cell value when clicks//
        buttonCol.Name = "ButtonColumnName";
        buttonCol.HeaderText = "Show";
        buttonCol.Text = "View";
        buttonCol.UseColumnTextForButtonValue = true;    
        dataGridView2.Columns.Add(buttonCol);

    }

    private void dataGridView2_buttonCol(object sender, DataGridViewCellEventArgs e)
    {

            string name = Convert.ToString(dataGridView2.SelectedRows[0].Cells[2].Value);
            ShowRichMessageBox("Code", name);

    }

编辑:

if (e.ColumnIndex != 0) // Change to the index of your button column
            {
                return;
            }

            if (e.RowIndex > -1)
            {
                string name = Convert.ToString(dataGridView2.Rows[e.RowIndex].Cells[2].Value);
                ShowRichMessageBox("Code", name);
            }
4

1 回答 1

3

传递给 CellClick 事件处理程序的DataGridViewCellEventArgs实例有一个ColumnIndex属性,您可以检查该单击是否来自按钮列。

像这样:

private void dgv_buttonCol(object sender, DataGridViewCellEventArgs e)
{
        if (e.ColumnIndex != 4) // Change to the index of your button column
        {
             return;
        }

        if (e.RowIndex > -1)
        {
            string name = Convert.ToString(dgv.Rows[e.RowIndex].Cells[2].Value);
            ShowRichMessageBox("Code", name);
        }
}

对于您问题的第二部分,我不确定您的意思,但您当然可以更改行高,也许在 SelectionChanged 事件中使用,或者如果您想更深入地做一些事情,请参阅“如何:自定义外观Windows 窗体 DataGridView 控件中的行数"

于 2012-05-25T11:48:19.850 回答