繁体   English   中英

如何获取单元格值按钮在DataGrid C#中单击

[英]How to get a cell value buttonclick in DataGrid C#

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

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

我有一些大的对数值,但已经在单元格中加载了,所以, Is it possible to expand the row when i select a particular row in the datagridview???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);
            }

传递给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 Forms DataGridView控件中的行数”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM