2011-03-01 82 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 
} 
相关问题