【问题标题】:GridView doesn't show dataGridView 不显示数据
【发布时间】:2014-10-07 12:31:31
【问题描述】:
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没有显示数据。

【问题讨论】:

  • 去掉这一行 dataGridView1.DataSource = "";并提供更多详细信息,以便我们为您提供帮助....
  • 我的登录名是... MrSQLInjection'); DROP TABLE LibraryInfo;-- P.s.不要使用我的名字;)
  • OP 唯一缺少的部分是dataGridView1.DataBind();
  • 请使用.DataBind()绑定数据
  • 您是否有不能使用GUI的原因?

标签: c# winforms gridview


【解决方案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;

  }

这个 sn-p 向您展示了如何 (a) 通过使用 Databind 方法将数据集绑定到网格视图。

该网站还说:

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

解决方案

我想引用以下语句:

 AuthorsGridView.DataBind();

实际上将数据绑定到控件。

旁注

为了保护自己免受 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;

【讨论】:

  • 你想用这个答案做什么?
  • 显示 OP 如何绑定 gridview,如问题中所述。我也可以问你为什么发表评论?
  • 写得很好,但它只适合asp.net。 OPs 的问题是关于 winforms,他指出(可悲!)迟到了......
  • 我已在问题下要求澄清,因此将编辑我的答案(取决于响应)
【解决方案2】:

此处缺少调用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

试试这种格式?

【讨论】:

  • GridView1.DataBind();部分有错误,好像无法识别.DataBind();
猜你喜欢
  • 2016-10-16
  • 2014-02-21
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
相关资源
最近更新 更多