2014-10-07 76 views
1
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沒有顯示數據。GridView不顯示數據

+1

刪除此行dataGridView1.DataSource =「」; ,包括更多的細節,以便我們可以幫助您... – 2014-10-07 12:35:05

+7

我的登錄名是...'MrSQLInjection'); DROP TABLE LibraryInfo; - 'P.s.請不要使用我的名字;) – Reniuz 2014-10-07 12:37:22

+1

OP缺少的唯一部分是'dataGridView1.DataBind();' – 2014-10-07 12:39:03

回答

1

採取於msdn文章在DataBind屬性。


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; 

    } 

此片段顯示瞭如何(一)數據集綁定到GridView,通過使用數據綁定方法分配它。

該網站還表示:

使用的DataBind()方法來從數據源到GridView控制數據綁定。此方法解析了控件的活動模板中的所有數據綁定表達式。

SOLUTION

我想引用線話說:

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; 
+0

你想用這個答案做什麼? – Tushar 2014-10-07 12:35:38

+1

顯示OP如何綁定gridview,如問題所述。我也可以問你爲什麼評論? – jbutler483 2014-10-07 12:37:05

+0

這很好寫,但它只適合於asp.net。 OP的問題是關於winforms的,他指出(可惜!)遲到... – DatRid 2014-10-07 13:15:54

0

使用缺少調用數據綁定下面的代碼方法here.Use:

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 

試試這種格式?

+0

GridView1的一部分存在錯誤。 DataBind();它似乎無法識別.DataBind(); – tokazaki 2014-10-07 12:59:10