窗户的DataGridView _RowCommand [英] Windows DataGridView _RowCommand

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

问题描述

我的背景是非常ASP.Net,我被要求开发一个小的Windows应用程序。我试图用网格来呈现,并选择数据,我艰难的,在窗口形式等同于ASP.Net的GridView控件是DataGridView中。我不知道但如果是这样的话,基本上在ASP.Net,你有相关的网格,单击命令按钮后触发的事件_RowCommand。我也注意到,有没有这样的东西作为一个DataKeyNames属性,所以我不知道如何通过当前行键点击的按钮。 !任何帮助将不胜感激,谢谢

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!

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

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

推荐答案

您正在寻找的事件是 CellClick 事件 - 以 DataGridViewButtonColumn 你实际上并不准事件处理程序的特定按钮,你会与你的窗体上的其他按钮

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.

这是由细胞click事件返回的事件参数,你就可以制定出点击是和你想采取什么样的行动哪一行和列。

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
}



超越那不过,我觉得你真的会采取一步,思考WinForms和WebForms的编码范例之间的差异中获益。

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.

通过web表单很多你代码的事实,一切是无状态决定的方式。这是不是与WinForms的情况下,你有状态,当你需要找出关于它们的信息,您可以访问大多数控件。

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.

由于这个原因,检索类的信息在一个DataGridView当前选择的小区是琐碎的WinForms。

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

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

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).

一个好地方让你过去的一些颠簸的可能是的 DataGridView的常见问题解答。它是有关式学习DataGridView的

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

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

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