从数据库填充Gridview [英] Fill Gridview from Database
本文介绍了从数据库填充Gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
有什么办法可以将数据从数据库获取到Gridview,但是我只想获取一列,而不是全部?您能帮我吗?
Hi guys,
Is there any way to get data from database to Gridview but i want to get only one column not all of them? Can you help me?
推荐答案
尝试一下:
从数据库获取数据到Gridview
Try this:
get data from database to Gridview
SqlCommand cm = new SqlCommand("Select * from table1", con);
SqlDataReader dr;
dr = cm.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dr);
//For winform
dataGridView1.DataSource = dataTable;
//For webform
GridView1.DataSource = dataTable;
GridView1.databind();
但是我只想得到一列,而不是全部
代替查询中的 * ,指定要在网格中显示的列名.
but i want to get only one column not all of them
Instead of * in query specify column name which you want to display in grid.
这篇关于从数据库填充Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文