在Datagridview中添加列 [英] Adding Column in Datagridview

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

问题描述

我有一个Windows窗体的Datagrid视图,并且使用这样的查询从数据库中获取了datagridview的内容.

I have a Datagrid view in windows form and the contents of datagridview are fetched from database using query like this.

//The primary Key is changed to SN using as function query here STARTS//////////

SqlCommand cmd = new SqlCommand("select CategoryId as SN, Name as CategoryName from Category", connect);
SqlDataAdapter abc = new SqlDataAdapter(cmd);

//The primary Key is changed to SN using as function query here ENDS//////////




上面的代码片段产生了以下内容:

SN分类名称
1 aaaaa
2 bbbbb
3 cccccc

SN实际上是主键!我想在自己的gridview中使用SN,并在查询中使用"AS"将主键修改为SN,但是我不希望使用主键,也不希望在数据库中使用任何类型的条目,例如SN.

我只希望SN在gridview中显示.我不知道在grid view中添加列的实际过程是什么.

非常感谢您的帮助.




The above code snippet produces this:

SN CategoryName
1 aaaaa
2 bbbbb
3 cccccc

Where SN is actually Primary key! I wanted SN in my gridview and modified primarykey as SN using ''AS'' in query but I dont want primary key be used neither want any type of entry like SN in database.

I just want SN to be displayed in gridview.I dont know what is the actual procedure to add column in grid view .

Any help is greatly appreciated.

推荐答案

这里有两种方法来获取数据库中实际上不存在的列.您可以像这样在SQL中提取常量:

SELECT '''' as MyNewStringColumn, 0 as MyNewNumericColumn, CategoryId as SN, Name as CategoryName FROM Category

然后,在填充数据表之后,遍历该表,并在表中填充所需的内容.但是请记住,您可以使用SQL中可访问的任何内容来填充它...因此,如果您的新列与SN相同,则可以在SQL中将其两次拉入并为其赋予不同的别名:
SELECT CategoryId as SN, Name as CategoryName, CategoryId as SN2 FROM Category

或者,如果您有一个公式,可以对其应用某种公式:
SELECT CategoryId as SN, Name as CategoryName, CategoryId + 1000 as SN2 FROM Category

添加数据库中不存在的列的另一种方法是...首先用您拥有的sql填充DataTable.然后,只需将另一列添加到DataTable中即可.之后,遍历表,并为每一行用所需的内容填充新列,如下所示:
(在vb中,但是您应该可以理解)
Here are two ways to get a column that doesn''t really exist in the database. You can just pull a constant in your sql...like this:

SELECT '''' as MyNewStringColumn, 0 as MyNewNumericColumn, CategoryId as SN, Name as CategoryName FROM Category

Then after you fill your DataTable, loop through it and fill the columns with what you want. But remember that you can fill that with anything accessible in SQL...so if your new column is the same thing as the SN you can just pull it twice in the SQL and give it a different alias:
SELECT CategoryId as SN, Name as CategoryName, CategoryId as SN2 FROM Category

Or if you can apply some kind of formula to it if you have one:
SELECT CategoryId as SN, Name as CategoryName, CategoryId + 1000 as SN2 FROM Category

Another method to add a column that doesn''t exist in the database is this...First fill the DataTable with the sql you have. Then simply add another column to the DataTable. After that, loop through your table and for each row fill the new column with whatever you want, like this:
(It''s in vb, but you should be able to get the idea)
'dt is the DataTable object
dt.Columns.Add("MyNewColumn")
Dim intCount As Integer = 1
For Each row As DataRow In dt.Rows
    row("MyNewColumn") = intCount
    intCount += 1
Next



如果您真正遇到的问题是您希望各列具有不同的标题...我认为您不能让DataTable列具有相同的名称,但是您可以为DataGridView对象中的列设置不同的标题文本.首先,使用名称相同的两列加载DataTable,然后将其设置为DataGridView的DataSource,然后执行以下操作:



If the problem your really having is that you want the columns to have different headings...I don''t think you can have DataTable columns with the same name, but you CAN set a different heading text to the columns in the DataGridView object. First you''d load your DataTable with two columns identical in all but name, then set it as the DataSource to your DataGridView, and then do this:

'dgv is the DataGridView object
dgv.Columns("SN").HeaderText = "SN"
dgv.Columns("SN2").HeaderText = "SN"



希望对您有所帮助.



Hope this helps.


尝试一下...

Try This...

SqlCommand cmd = new SqlCommand("select CategoryId as SN, Name as CategoryName from Category", connect);
SqlDataAdapter abc = new SqlDataAdapter(cmd);
DataTable myTable=new DataTable();
abc.Fill(myTable);
Datagridview1.DataSource=dt;


这篇关于在Datagridview中添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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