0

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!

3
Contributors
12
Replies
13
Views
5 Years
Discussion Span
Last Post by anthonyjpv
0

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.

Edited by kamilacbe: n/a

0

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

0

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!

0

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();
           

        }
0

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

0

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

0

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!

0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.