I know how to pull data from a db and present it in a textbox in a form

my question is, how can i pull data from a db and present it in a datagridview?

and another thing,

once a certain row is double clicked, new form will show and a data will be sent to that ready to be edited.

Thanks!

Dani AI

Generated

Quick summary and a few practical patterns that fill gaps left by the replies here: bind your query results to a middle layer (a BindingSource or a list of small DTOs) rather than reading cells by hard-coded indexes. That makes selection, filtering and getting the primary key stable even if column order changes. As pointed out, binding the result to the grid is the right starting point; the next step is to treat the grid as a view of your data source, not the store of truth.

How to get the primary key reliably: read it from the bound item (cast the grid row’s DataBoundItem to a DataRowView or your object and read the ID column/property by name). If the grid is unbound, take the selected row’s cell that holds the ID by column name. Pass that ID into the editor/detail form (prefer constructor or a public ID property) and have the detail form re-query the database for the single record — this avoids stale data and race conditions.

Make the grid selection-only and less “editable” for better UX: use full-row selection, single selection, set read-only/edit mode to avoid in-cell edits, and turn off the automatic add-row if you don’t want the empty new-row placeholder. If you truly want an extra bottom row, either enable the grid’s built-in add-row feature (for bound sources) or append a new DataRow to the underlying DataTable / add a DataGridViewRow when the grid is unbound. For a cleaner workflow, consider a dedicated “New” button that opens an empty editor form and refreshes the grid after the insert.

Final tips: avoid SELECT * and loading huge tables into the UI (use paging or server-side filters), always use parameterized queries and using blocks for DB objects, and hook BindingSource.CurrentChanged to keep detail controls in sync instead of indexing cells. This keeps the UI robust and maintainable years after the original code was written.

Recommended Answers

All 12 Replies

Can u explain little bit more it's hard to understand

hi
to display your data in gridview without any modyfication you can go for datareader or go for adapter to fill the data and bind it to gridview using datasource

con.Open();
            cmd.Connection = con;
            SqlDataAdapter da = new SqlDataAdapter("select * from tablename ", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "Tablename");
         gridviewname.DataSource = ds.Tables["tablename"];
            con.Close();

for the secound question double click your gridview and pass event ..in that send the value to a new form as required .

hope this would help you.

Thanks! how to get the row value of the selected gridview record then?

where do you want to display the selected value from the gridview
..is it to individual controls or in a new form .

Im planning to add a new form or i can just simply add textboxes on the same form.

what i want to know though is the method name if a certain row in a datagrid view is selected?

and event name for double clicking the selected row so i can pass in the records to a new form?

by the way. is there a chance to disable the cursor from displaying in the datagrid view once record is clicked? or act as a read only to avoid the conception of being able to edit a record.

and one more thing, how to add one row at the bottom of the datagrid?

so many questions!

Thanks for the prompt answer mate!

try this ...one way to select the row data to respective controls

private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            listBox1.Items.Clear();
            textBox1.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            textBox2.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBox3.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
            comboBox2.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
            textBox5.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
            //maskedTextBox1.Text = this.dataGridView1.CurrentRow.Cells[5].Value.ToString();
            textBox7.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString();
            textBox8.Text = this.dataGridView1.CurrentRow.Cells[7].Value.ToString();
            comboBox1.Text = this.dataGridView1.CurrentRow.Cells[8].Value.ToString();
           

        }

i see what you got. thanks!

never mind the event name, i got it!

my question then are:

getting the primary key of the selected row?

thanks!

and

do not allow editing on the cell

hi..
primary key will be your column name so call that column particularly, secondly go for read only property to avoid editing data grid.

I figured out whats the event name

I figured out how to disable cursor

I figured out how to read only

last question: whats the code to fetch the record by selected datagrid row?

thanks!

mark this as solved since I got all i need from the web. thanks!

Hi..
the above code will help you to fetch the selected record in controls using gridview click event ..
Can you be precise in your question please.

I figured it out using rouheader click event. thanks for your prompt reply!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.