Click here to Skip to main content
15,171,576 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello tell me how you can output from the database in Datagriedview, I did this, but I output only 1 line in this way, and I need to automatically output with this view all the records found, for example id=3, then outputs 2 stores, but only 1 store is output like this $ "\nMagazin: "+ (dt.Rows[Ctrw][0]) + "\n Date: "+ (dt.Rows[Ctrw][1]) + "\n", but how can I make 2 stores in the same format appear? I know what I have bound to this dataGridView2.Rows[0].Cells[0].Value therefore outputs the current 1 line, but here's how you can make all the lines output I can't think of(

What I have tried:

<pre lang="C#">
public void Conclusion()
            string constr = @"Data Source=DESKTOP-3FE5J7L;Initial Catalog=Shop;Integrated Security=True";
            SqlConnection con = new SqlConnection(constr);
            SqlCommand com = new SqlCommand("SELECT Name, Datareg FROM Dog INNER JOIN Shop on = Shop.Dog_id Inner join Prod on Shop.prod_id = Where = @id", con);
            com.Parameters.AddWithValue("@id", id);
            DataTable dt = new DataTable();
            SqlDataReader sdr = com.ExecuteReader();
            dataGridView2.DataSource = dt;
            dataGridView2.Columns[0].Width = 317;
            dataGridView2.Columns[1].Visible = false;
            if (dt.Rows.Count != 0)
                int Ctrw = dt.Rows.Count;
                Ctrw = 0;
                dataGridView2.Rows[0].Cells[0].Value = $"\nМагазин: " + (dt.Rows[Ctrw][0]) + "\n                                 Дата: " + (dt.Rows[Ctrw][1]) + " \n";
Updated 4-Apr-21 9:09am

1 solution

Start by using the debugger to do two things:
1) Look at the content of id when you set the parameter value.
If it's not what you expect, you will not get the rows you expect.
2) Once loaded, look at the DataTable and see how many rows it contains before you start using it.
If it contains no rows, you know why you get nothing displayed...

Then start looking at your data to find out what you expected to get from the SELECT operation.

We can't do any of that for you: we have no access to your data or database!

But do yourself a favour: don't hardcode connection strings. Always use a config file or similar as otherwise you need to change your code between dev and release, and that means releasing untested software.
I do this: Instance Storage - A Simple Way to Share Configuration Data among Applications[^] but it's probably a little overkill for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900