如何在Gridview中进行数据绑定?
问题描述:
我正在尝试在单击按钮时为网格视图绑定数据
并在网格视图中显示表数据...
但是我不知道要为此Asp.net和ms sql server 2005编写的代码
请逐步告诉我该怎么做....
I am trying to bind data for grid view on button click
and also display the table data in the grid view...
But i do not know the code to be written for this Asp.net and ms sql server 2005
plz tell me in steps what to do....
答
Rathi ,,
请尝试以下代码.
Hi Rat,
Please try like below code.
Using(SqlConnection con=new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["master"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from userinfo ", con);
DataTable dt = new DataTable();
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(dt);
GridView1.DataSource = null;
GridView1.DataSource = dt;
GridView1.DataBind();
}
您可能会有所帮助
It may be help u