簡體   English   中英

C#:在網格中顯示數據的最佳方法?

[英]C#: best way to show data in grid?

我正在使用C#WinForms。 我想在網格中顯示數據。 網格必須能夠響應對行的單擊。 最好使用什么組件?

他正在談論一個類似於按鈕單擊的事件。 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");
}

DataGridView的。 但是我不明白您的意思是“他的網格必須能夠響應行的點擊”。

編輯:您可以在datagridview中使用各種事件來跟蹤單擊的行,列和單元格。 Gridview還支持“列”中的諸如Button,LinkBut​​ton和Image之類的控件。

您必須無法使數據網格的整個行都具有選擇屬性,然后在行單擊或雙擊(無論您想要什么)事件上添加代碼。

在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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM