DataGrid中的可编辑单元格 [英] Editable cells in a DataGrid

查看:178
本文介绍了DataGrid中的可编辑单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计,



我需要一个用户可以编辑单元格的表格,就像在Microsoft Excel中一样。我需要~3x20个空白单元格,用户可以在其中添加数据。用户应该单击Cell并使用他的键盘编辑Cell。

DataGrid是否适合这样做?我怎么才能意识到这一点?



感谢您的帮助!

解决方案

Hello Brother,

引用:

用户应单击Cell并使用键盘编辑Cell。

DataGrid是否适合这样做?我怎么能意识到这一点?



你可以使用TablelayoutPanelControl来做到这一点。我觉得它更灵活(如果我错了,请纠正我)。

在tablelayoutpanel控件中,您可以添加自己喜欢的控件来生成模板。我甚至可以生成带有文本框和按钮的表单。

在你的情况下你想要生成一个包含3行和20列的表格,这样:



表格载入:

 private void Form1_Load(object sender,EventArgs e)
{
tableLayoutPanel1.ColumnCount = 20; ////从较低的值开始说5,因为你必须通过拖动它的边界来扩展你的tablelayoutpanel控件以获得更大的值
tableLayoutPanel1.RowCount = 3;
for(int i = 0; i< 20; i ++)
{
for(int j = 0; j< 3; j ++)
{
tableLayoutPanel1.Controls.Add(new TextBox(),i,j);
}

}
}



- 但请注意当你将tablelayoutpanel控件添加到表单,确保您自动调整其行和列。因为默认情况下tablelayoutpanel控件只呈现2列。

- 要做到这一点,你需要右键单击tablelayoutpanel选择rows选项并将size模式设置为autosize。类似地,你也需要为列做这个。

- 再次检查是否已经设置了行和列的自动调整大小。如果没有,请再次执行。

- 如果行数和列数增加,那么在呈现应用程序时可能会达到非响应状态。所以你需要使用后台工作者。我刚刚建议你一个控件和逻辑。

- 对FormLoad(我在这里做过)​​执行任何操作也是一个非常错误的事情,最好是在后台初始化一个线程,线程将呈现窗体上的控件。使用backgroundworker。请深入了解后台工作人员。当你想在UI上渲染很多东西时它非常有用并且也可以执行任何操作。



参考:

初学者的BackgroundWorker类示例 [ ^ ]



请不要mind:一个小作业:尽量使这个应用程序动态化。接受文本框中的行数和列数:)

请告诉我我发布的代码是否有效?



谢谢,

- Rahul


我建​​议 WPF DataGrid实际示例 [ ^ ]

Hey Guys,

I need a table where the User can edit cells, like in Microsoft Excel. I need ~3x20 blank Cells where the User can Add his Data. The User should click on the Cell and edit the Cell by using his keyboard.
Is a DataGrid the right thing for doing this? And How can I realize this?

Thanks for your help!

解决方案

Hello Brother,

Quote:

The User should click on the Cell and edit the Cell by using his keyboard.
Is a DataGrid the right thing for doing this? And How can I realize this?


You can do this using TablelayoutPanelControl. I feel it is much more flexible(Please correct me if i am wrong).
In a tablelayoutpanel control you can add controls you like to generate a template. I can even generate a form with textboxes and buttons.
In your case you want to generate a table of 3 rows and 20 columns so:

On Form Load:

private void Form1_Load(object sender, EventArgs e)
       {
           tableLayoutPanel1.ColumnCount = 20; ////start with a lower value say 5, because you will have to expand your tablelayoutpanel control by dragging its boundries for bigger value
           tableLayoutPanel1.RowCount = 3;
           for (int i = 0; i < 20; i++)
           {
               for (int j = 0; j < 3; j++)
               {
                   tableLayoutPanel1.Controls.Add(new TextBox(), i, j);
               }

           }
       }


- But please note when you add a tablelayoutpanel control to the form, make sure that you "autosize" both its rows and columns. Because by default the tablelayoutpanel control only renders 2 columns.
- To do that you need to right click on the tablelayoutpanel select the rows option and set the size mode to autosize. Similarly you need to do that for columns also.
- Again recheck if the Autosize for Rows and Columns has been set or not. If not, do it again.
- If the number of rows and columns increase then while rendering the application may reach into a non responsive state. So you need to use a background worker for that. I have just suggested you a control and the logic.
- Also its a very wrong thing to perform any action on FormLoad (which i have done here)best is to initialize a thread in the background, that thread will render the control on the form. Use backgroundworker. Please read about background worker in depth. Its very helpful when you want to render many things on UI amd perform any operation also.

Refer:
BackgroundWorker Class Sample for Beginners[^]

And please dont mind: A small homework: Try to make this application dynamic. Accept the row and column count from textboxes :)
Please do tell me whether the code i posted works or not?

Thanks,
- Rahul


I would suggest WPF DataGrid Practical Examples[^]


这篇关于DataGrid中的可编辑单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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