Windows DataGridView _RowCommand [英] Windows DataGridView _RowCommand

查看:81
本文介绍了Windows DataGridView _RowCommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的背景是ASP.Net,我被要求开发一个小的Windows应用程序。我试图使用一个网格来呈现和选择数据,而我很难把Windows窗体中相当于ASP.Net的GridView的是DataGridView。我不确定是否这样,基本上,在ASP.Net中,您有_RowCommand事件与网格相关联,这是在单击Commandbutton之后触发的。我也注意到没有像DataKeyNames属性这样的东西,所以我不知道如何将当前行键传递到点击的按钮。任何帮助将不胜感激,谢谢!



我忘了提到:我的网格有两个DataGridViewButton类型的列,我不知道我需要代码的事件开始执行所选命令

解决方案

您要查找的事件是 CellClick 事件 - 使用 DataGridViewButtonColumn 您实际上并不会将事件处理程序与特定按钮相关联,就像您与表单上的其他按钮一样。



从单元格单击事件返回的事件参数中,您可以确定点击所在的行和列,以及要执行的操作。

  private void dataGridView1_CellClick(object sender,DataGridViewCellEventArgs e)
{
DataGridViewButton cell =(DataGridViewButtonCell)
dataGridView1.Rows [e.RowIndex ] .Cells [e.ColumnIndex];

//任何额外的逻辑你想要
}

超越然而,我认为您将真正受益于退后一步,并考虑到winforms和webforms编码范例之间的差异。



对于Webforms来说,很多方法代码都是由一切都是无状态的事实决定的。这不是winform的情况,您有状态,您可以访问大多数控件,当您需要找到有关它们的信息。



因此,检索信息,如DataGridView中当前选定的单元格与winforms相当微不足道。



此外,在大多数情况下,使用winforms您不需要特定按钮进行编辑或删除 - 您可以直接在网格中进行编辑,并使用内置的删除功能(选择行,然后按删除键)。



一个好的地方,让你过去一些颠簸可能是 DataGridView常见问题。对于DataGridView


来说,这是一个惊人的资源

My background is pretty much ASP.Net, and I was asked to develop a small windows application. I tried to use a grid to present and select data, and I tough that the equivalent in windows forms to the ASP.Net's GridView was DataGridView. I'm not sure yet if that is the case, basically, in ASP.Net, you have the _RowCommand event associated to the grid, that is triggered after a Commandbutton is clicked. I noticed also that there is no such thing as a DataKeyNames property, so I don't know how to pass the current row key to the button clicked. Any help will be appreciated, thanks!

I forgot to mention: My grid has two DataGridViewButton type of columns, And I don't know the event that I need to code on to perform the selected command

解决方案

The event you are looking for is the CellClick event - with the DataGridViewButtonColumn you do not actually associate event handlers to the particular buttons as you would with other buttons on your form.

From the event arguments returned by the cell click event you can then work out which row and column the click was in and from that what action you want to take.

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{        
    DataGridViewButton cell = (DataGridViewButtonCell)
        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];

    // Any additional logic you want
}

Beyond that however, I think you would really benefit from taking a step back and thinking about the differences between the winforms and webforms coding paradigms.

With webforms a lot of the way you code is dictated by the fact that everything is stateless. This is not the case with winforms, you have state and you can access most controls when you need to find out information about them.

For this reason, retrieving information like the currently selected cell in a DataGridView is trivial with winforms.

Also, in most cases with winforms you do not need specific buttons to edit or delete - you can edit directly in the grid, and use in inbuilt delete functionality (select the row and press the delete key).

A good place the get you past some of the bumps might be the DataGridView FAQ. It's an amazing resource for learing about the DataGridView

这篇关于Windows DataGridView _RowCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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