2014-10-07 3 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 = ""; 그리고 우리가 당신을 도울 수 있도록 더 자세한 내용을 포함 .... –

+7

내 로그인 이름은 ... ... MrSQLInjection); DROP TABLE LibraryInfo; -'P.s. 내 이름을 사용하지 마십시오;) – Reniuz

+1

OP가 누락 된 유일한 부분은'dataGridView1.DataBind(); '입니다. –

답변

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; 

    } 

이 조각 (a)는 DataBind 메서드를 사용하여 할당함으로써,있는 gridview에 데이터 집합을 바인딩하는 방법을 보여줍니다.

이 사이트는 말한다 :

가 GridView 컨트롤에 데이터 소스에서 데이터를 바인딩 할 데이터 바인딩() 메서드를 사용합니다. 이 메서드는 컨트롤의 활성 템플릿에있는 모든 데이터 바인딩 식을 확인합니다.

해결 내 말은 선을 참조하고 싶은

: 실제로 이 컨트롤에 데이터를 결합

AuthorsGridView.DataBind(); 

.

SIDE 참고 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

+1

OP에서 질문에 명시된대로 그리드 뷰를 바인딩하는 방법을 보여줍니다. 나는 또한 왜 당신이 논평 한 것과 동일한 것을 물을 수 있습니까? – jbutler483

+0

멋지게 작성되었지만 asp.net에만 적합합니다. OP 질문은 늦게까지 (슬프게도!) 늦게까지 지적한 winforms에 관한 것입니다 ... – DatRid

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()를 recoginize 할 수없는 것 같습니다; – tokazaki

관련 문제