[英]how to bind data to gridview in windowsApplication C#
I have the below code in button. 我在按钮中有以下代码。 By clicking that button , i retrieve data from sql server and to display in GridView. 通过单击该按钮,我从sql server中检索数据并显示在GridView中。 I can retrieve and filled in dt.But my data are not shown in grid view, What is theProblem? 我可以检索并填写dt。但是我的数据没有显示在网格视图中,什么是问题? please crack me out. 请把我弄出来。
private void gvIPretrieve_Click(object sender, EventArgs e)
{
connstring();
conn.Open();
using (SqlCommand cmd = new SqlCommand("gvretrieve_ip", conn))
{
cmd.CommandType = CommandType.StoredProcedure ;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView2.DataSource = dt;
}
}
}
}
确保Windows应用程序中没有Gridview。有一个控件以行和列的形式显示数据,即DataGridView。这是从SQL Server数据库通过数据表绑定数据的最佳有用链接 。
尝试使用dataGridView2.DataSource = dt.DefaultView;
Sir, asp.net or windows? 先生,asp.net还是Windows? You need to add dataGridView2.DataBind();
您需要添加dataGridView2.DataBind();
after dataGridView2.DataSource=dt;
在dataGridView2.DataSource=dt;
Hope, it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.