M
M
Michael2016-01-04 22:01:23
SQL
Michael, 2016-01-04 22:01:23

How to generate sql query from datagridview c#?

How to teach DGV to form the query I need? That is, there is 1 DGV row, for example, 2 variables (if there are 2 columns) there is a pre-prepared sql query in which key data is missing, and this data is obtained from the DGV row.
It comes out something like this:

INSERT INTO cli (name, phone) values('ЗНАЧЕНИЕ ИЗ СТОЛБЦА 1', 'ЗНАЧЕНИЕ ИЗ СТОЛБЦА 2')

And by pressing the button, this request goes away (I know how to send requests by button xD)
I would be very grateful to someone who will tell you how or give a link where it has already been. I climbed on Google - a trifle (

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VZVZ, 2016-01-04
@VZVZ

> How to teach DGV to form the query I need?
The grid itself does not know how to form anything.
There is nothing like this in .NET, THAT high-level.
You need to get the selected grid row yourself, then the values ​​from its cells, and finally compose the SQL query.
Moreover, the selected row can be either dataGridView1.CurrentRow or dataGridView1.SelectedRows (depending on where exactly the user clicks), so you need to implement both options, with checks for null.
Variant with dataGridView1.CurrentRow:
Variant with dataGridView1.SelectedRows:

MessageBox.Show(dataGridView1.SelectedRows[0].Cells[0].Value.ToString()); // Cells[0] - первый столбец

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question