如何在datagridview winform C#中填充其id的值 [英] How to fill values from its id in datagridview winform C#

查看:131
本文介绍了如何在datagridview winform C#中填充其id的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个Datagridview。它有三列,如



FruitId | FruitPrice | FruitName

1 | 100 |

2 | 200 |

3 | 300 |



这些值来自table_FruitPrice

现在最后我想通过第一列中显示的FruitId在第三列FruitName中显示和FruitName将来自Table_FruitName。



加入方法可以正常工作并且正常工作。但是如果我点击一个按钮,我想在第三列显示Fruitname。



Plz sir help ...



我的尝试:



.................. ................

There is one Datagridview. It has three columns like

FruitId | FruitPrice | FruitName
1 | 100 |
2 | 200 |
3 | 300 |

these value comes from table_FruitPrice
Now finally I want to display in third column FruitName by its FruitId that is presenting in column one and FruitName will come from Table_FruitName.

joining method can work and it is working. But I want to display Fruitname in third column if I click on a button.

Plz sir help...

What I have tried:

..................................

推荐答案

您必须从数据库中获取相应的数据:

You have to get corresponding data from database:
SELECT fp.FruitId, fp.FruitPrice, fn.FruitName
FROM table_FruitPrice AS fp INNER JOIN Table_FruitName AS fn ON fp.FriutId = fn.FruitId





如何获取数据?请参阅:

使用DataReader检索数据 [ ^ ]

演练:使用ADO创建简单的数据应用程序。 NET [ ^ ]


不是一个很好的解决方案,但它应该完成工作并给你一些想法。
Not really a nice solution but it should do the job and give you some ideas.
FormLoad:
// Your SELECT will be
SELECT fp.FruitId, fp.FruitPrice, fn.FruitName, '' AS FruitNameEmpty
FROM table_FruitPrice AS fp INNER JOIN Table_FruitName AS fn ON fp.FriutId = fn.FruitId
// Don't show the "real" fruitname
dataGridView.Columns[2].Visible= false;
// Adjust header text for the empty fruitname column
dataGridView.Columns[3].HeaderText= dataGridView.Columns[2].HeaderText;




OnButtonClick:
// E.g. Toggle visibility
dataGridView.Columns[2].Visible= !dataGridView.Columns[2].Visible;
dataGridView.Columns[3].Visible= !dataGridView.Columns[3].Visible;



注意:您应该通过FieldName搜索列来替换Column的直接地址(即Column [2]等)。



一个更好的解决方案,但还有更多需要学习的方法是实现DataGridViews的CellPainting事件,它根据你的状态绘制列。



我希望它有所帮助。


Note: You should replace the direct adressing of Column (i.e. Column[2] etc.) by search the column by FieldName.

A more nice solution, but also something more to learn to do it will be to implement DataGridViews's CellPainting event, which draws the column according to your state.

I hope it helps.


这篇关于如何在datagridview winform C#中填充其id的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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