2011-03-01 57 views
2

我正在使用C#WinForms。我想在網格中顯示數據。網格必須能夠響應行上的點擊。什麼是最好的組件使用?C#:在網格中顯示數據的最佳方式?

+0

在.NET中內置的DataGrid會爲你工作得很好。 您是否想要關於如何將數據綁定到DataGrid的示例代碼? – 2011-03-01 09:10:20

+0

有請示例代碼。 – 2011-03-01 09:13:53

+0

這不是在這篇博客文章討論的內容的範圍內嗎? http://blog.stackoverflow.com/2011/02/are-some-questions-too-simple/ – Mayank 2011-03-01 09:21:24

回答

4

他很長時間在談論類似於按鈕點擊的事件。 DataGridView應該能夠包含像下拉菜單這樣的控件,因此您將能夠添加一個依賴於所選單元格的響應。

嘗試

private void GetData(string selectCommand) 
{ 

     // Specify a connection string. Replace the given value with a 
     // valid connection string for a Northwind SQL Server sample 
     // database accessible to your system. 
     String connectionString = 
      "Integrated Security=SSPI;Persist Security Info=False;" + 
      "Initial Catalog=Northwind;Data Source=localhost"; 

     // Create a new data adapter based on the specified query. 
     dataAdapter = new SqlDataAdapter(selectCommand, connectionString); 

     // Create a command builder to generate SQL update, insert, and 
     // delete commands based on selectCommand. These are used to 
     // update the database. 
     SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); 

     // Populate a new data table and bind it to the BindingSource. 
     DataTable table = new DataTable(); 
     table.Locale = System.Globalization.CultureInfo.InvariantCulture; 
     dataAdapter.Fill(table); 
     bindingSource1.DataSource = table; 

     // Resize the DataGridView columns to fit the newly loaded content. 
     dataGridView1.AutoResizeColumns( 
      DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); 

} 

private void Form1_Load(object sender, System.EventArgs e) 
{ 
    // Bind the DataGridView to the BindingSource 
    // and load the data from the database. 
    dataGridView1.DataSource = bindingSource1; 
    GetData("select * from Customers"); 
} 
0

DataGridView。 但我不明白你的意思是「他的網格必須能夠響應點擊行」。

編輯:您可以使用datagridview中的各種事件來跟蹤哪個行,哪個列以及您單擊了哪個單元格。此外,Gridview支持列中的按鈕,鏈接按鈕和圖像等控件。

+0

我將有另一個數據網格,顯示其他數據取決於第一個網格點擊哪一行。 – 2011-03-01 09:13:12

+0

@克萊格約翰斯頓:我修改了答案。 – Anuraj 2011-03-01 09:17:00

0

必須無法在數據網格的整個行選擇屬性,然後在行單擊或雙擊(任何你想要的)事件中添加代碼。

0

在DataGrid中你有一個事件。如果你在下面做這樣的事情,你可以管理CLick。

public Form1() 
{ 
    InitializeComponent(); 
    dataGridView1.RowStateChanged += new DataGridViewRowStateChangedEventHandler(dataGridView1_RowStateChanged); 
} 

void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e) 
{ 
    DataGridViewRow dgvr = e.Row; 

    //GetDataFrom Database to fill other Grid 
} 
相關問題