English 中文(简体)
如何在 DataGrid C # 中获取一个单元格值单击的按键的单元格值?
原标题:How to get a cell value buttonclick in DataGrid C#

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

我怎样才能解决上述问题?

I have some logvalues which are big but it s already loaded in the cell so, 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);
            }
最佳回答

< a href=" "http://msdn.microsoft.com/ en- us/library/ system.windows.forms.datagridviewcelleveventargs.aspx" rel="nofollow" >DataGridViewCellEventArgs 传给CellClick事件处理器的示例有一个 < a href=" "http://msdn.microsoft.com/ en-us/library/ systems.windows.forms.dgoldgridgridviewceleventargs.columindex.aspx" rel=“nofolpolt" >ColumIndex 属性,您可以查看点击是否来自按钮栏。

像这样:

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);
        }
}

对于你问题的第二部分,我不知道你的意思是什么,但你肯定可以改变行高度,也许在选择更改的事件中使用,或者如果你想做更深入的事情,请参看,http://msdn.microsoft.com/en-us/library/85kxkk29c.aspx" rel=“nofollow””“如何:自定义窗口形式 DataGridView Control 中行的外观”

问题回答

暂无回答




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签