GridView不显示数据

时间:2014-10-07 12:31:31

标签: c# winforms gridview

private void fill()
{
  adptr = new OleDbDataAdapter(@"SELECT * FROM LibraryInfo WHERE First_Name='"+LoginName+"'", cn);      
  //LoginName is a string variable for displaying users info after the login          
  ds.Clear();
  adptr.Fill(ds);
  dataGridView1.DataSource = "";
  dataGridView1.DataSource = ds.Tables[0];
}

数据库更新后,GridView没有显示数据。

2 个答案:

答案 0 :(得分:1)

取自DataBind财产上的这篇msdn文章。


ASP示例


 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;

  }

此片段向您展示如何(a)将数据集绑定到gridview,方法是使用Databind方法进行分配。

该网站还说:

  

使用DataBind()方法将数据从数据源绑定到GridView控件。此方法解析控件的活动模板中的所有数据绑定表达式。

<强>解

我想参考一下这句话:

 AuthorsGridView.DataBind();

实际上数据绑定到控件。

SIDE NOTE

为了保护自己免受SQLi攻击,您应该阅读SQL Parameters,而不是直接连接。


WINFORM示例


找到了教程:here

//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;

答案 1 :(得分:0)

此处缺少使用databind方法。使用以下代码:

 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

试试这种格式?