Craig Johnston
Craig Johnston

Reputation: 7607

C#: best way to show data in grid?

I am using C# WinForms. I want to display data in a grid. The grid must be able to respond to clicks on rows. What is the best component to use?

Upvotes: 2

Views: 4554

Answers (4)

Six Beacon Gaia
Six Beacon Gaia

Reputation: 170

He's prolly talking about an event similar to button click. DataGridView should be able to contains controls like dropdown menu, so you will be able to add a response dependent on the selected cell.

Try

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");
}

Upvotes: 4

Robbie Tapping
Robbie Tapping

Reputation: 2556

In the DataGrid you have an Event. If you do something like this below you can manage the 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
}

Upvotes: 0

Bhavik Goyal
Bhavik Goyal

Reputation: 2796

You must unable the whole row select property of the data grid and then add the code on row click or double click (whatever you want) event.

Upvotes: 0

Anuraj
Anuraj

Reputation: 19618

DataGridView. But I don't understand what you mean by "he grid must be able to respond to clicks on rows".

Edit : You can use various events in datagridview to track, which row, which column, and which cell you are clicked. Also Gridview supports controls like Button, LinkButton, and Image in the Columns.

Upvotes: 0

Related Questions