C# GridView没有';不显示数据
数据库更新后,GridView没有显示数据。摘自本文的C# GridView没有';不显示数据,c#,winforms,gridview,C#,Winforms,Gridview,数据库更新后,GridView没有显示数据。摘自本文的DataBind属性 ASP示例 此代码段向您展示了如何(a)通过使用Databind方法分配数据集,将数据集绑定到gridview 该网站还说: 使用DataBind()方法将数据源中的数据绑定到GridView控件。此方法解析控件的活动模板中的所有数据绑定表达式 解决方案 我想引用这句话: void Page_Load(Object sender, EventArgs e) { // This example uses
DataBind
属性
ASP示例
此代码段向您展示了如何(a)通过使用Databind方法分配数据集,将数据集绑定到gridview 该网站还说: 使用DataBind()方法将数据源中的数据绑定到GridView控件。此方法解析控件的活动模板中的所有数据绑定表达式 解决方案 我想引用这句话:
void Page_Load(Object sender, EventArgs e)
{
// This example uses Microsoft SQL Server and connects
// to the Northwind sample database. The data source needs
// to be bound to the GridView control only when the
// page is first loaded. Thereafter, the values are
// stored in view state.
if(!IsPostBack)
{
// Declare the query string.
String queryString =
"Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]";
// Run the query and bind the resulting DataSet
// to the GridView control.
DataSet ds = GetData(queryString);
if (ds.Tables.Count > 0)
{
AuthorsGridView.DataSource = ds;
AuthorsGridView.DataBind();
}
else
{
Message.Text = "Unable to connect to the database.";
}
}
}
DataSet GetData(String queryString)
{
// Retrieve the connection string stored in the Web.config file.
String connectionString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;
DataSet ds = new DataSet();
try
{
// Connect to the database and run the query.
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
// Fill the DataSet.
adapter.Fill(ds);
}
catch(Exception ex)
{
// The connection failed. Display an error message.
Message.Text = "Unable to connect to the database.";
}
return ds;
}
实际上,将数据绑定到控件
旁注
为了保护自己不受SQLi的影响,您应该仔细阅读,而不是直接连接
WINFORM示例
找到了教程: 此外:
此处缺少用于调用数据绑定方法的Use。请使用以下代码:
//the DataGridView
DataGridView dgView = new DataGridView();
//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();
//set the BindingSource DataSource
bSource.DataSource = dTable;
//set the DataGridView DataSource
dgView.DataSource = bSource;
尝试使用此格式?删除此行dataGridView1.DataSource=“”;并包括更多详细信息,以便我们可以帮助您…我的登录名是<代码>MrSQLInjection');DROP TABLE LibraryInfo--代码>注意不要使用我的名字;)OP唯一缺少的部分是
dataGridView1.DataBind()代码>请使用.DataBind()绑定数据是否有不能使用的原因?您试图如何处理此答案?演示如何绑定gridview,如问题中所述。我也可以问你为什么要发表评论?写得很好,但它只适用于asp.net。OPs的问题是关于winforms的,他很晚才指出(很遗憾!)。我已经在问题下要求澄清,因此将编辑我的答案(取决于回答)。GridView1.DataBind()的一部分中有错误;,似乎它无法重新编码.DataBind();
AuthorsGridView.DataBind();
//create the connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\myDatabase.mdb";
//create the database query
string query = "SELECT * FROM MyTable";
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
//create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);
//create a DataTable to hold the query results
DataTable dTable = new DataTable();
//fill the DataTable
dAdapter.Fill(dTable);
//the DataGridView
DataGridView dgView = new DataGridView();
//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();
//set the BindingSource DataSource
bSource.DataSource = dTable;
//set the DataGridView DataSource
dgView.DataSource = bSource;
DataAdapter adapter=new DataAdapter(SqlCommand,SqlConn);
DataTable tbl=new Datatable();
adapter.Fill(tbl);
GridView1.DataSource=tbl;
GridView1.DataBind();//This line is missing in your code