简体   繁体   中英

How do i choose the column that i want to add data into a DataGridView?

I have this Source Code:

private void button1_Click(object sender, EventArgs e)
    {
        dataGridView1.Visible = true;
        dataGridView2.Visible = true;
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Olimpiada\SistemSolar\SistemSolar\DBSistem.mdf;Integrated Security=True;User Instance=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT Denumire FROM Caracteristici", con);
        SqlCommand cmd1 = new SqlCommand("SELECT Valoare FROM Valori", con);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            string[] RowData = { sdr.GetString(0) };
            dataGridView2.Rows.Add(RowData);
        }
        sdr.Close();
        //SqlCommand cmd1 = new SqlCommand("SELECT Valoare FROM Valori", con);
        SqlDataReader sdr1 = cmd1.ExecuteReader();
        while(sdr1.Read())
        {
            string[] RowData1 = { sdr1.GetString(0) };
            dataGridView2.Rows.Add(RowData1);
        }
        sdr1.Close();
        SqlCommand cmd2 = new SqlCommand("SELECT UM FROM Caracteristici", con);
        SqlDataReader sdr2 = cmd2.ExecuteReader();
        while (sdr2.Read())
        {
            string[] RowData2 = { sdr2.GetString(0) };
            dataGridView2.Rows.Add(RowData2);
        }
        sdr2.Close();

    }

And I have a question about this: How can choose,after i finished inserting RowData,to move to the other column? Because with this code all data is only on a single Column.

The problem is that when you are adding your data to the row you are adding it like this:

string[] RowData = { sdr.GetString(0) };
dataGridView2.Rows.Add(RowData);

This is always adding the data to the first column. To add the data to other columns you need to add the data the the column index that you require eg

string[] RowData = { "Column1", sdr.GetString(0), "Column3" };

Will add data to the second column if there are 3 columns.

https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows(v=vs.110).aspx

This is an idea, I hope you get it ! This example will add a row with all columns filled:

foreach (var item in Logs)
{
    string[] row = new string[] { item.Id, item.Name, item.Description };

    dataGridView.Rows.Add(row);
    dataGridView.ClearSelection();
    dataGridView.Rows[dataGridView.Rows.Count - 1].Selected = true;
    dataGridView.FirstDisplayedScrollingRowIndex = dataGridView.Rows.Count - 1;
}

The order of string[] row = new string[] { item.Id, item.Name, item.Description }; should be equal to your datagridView columns order.

Logs in my code is a List<Log> , but should work with any other collection type.

If logs are filled with various objects, this example will add the same amount of rows.

I solved the problem.I will leave the solution here:

string[] x = new string[100];
        string[] y = new string[100];
        string[] z = new string[100];
        string[] den = new string[100];
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DBSistem.mdf;Integrated Security=True;User Instance=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT Denumire FROM Caracteristici", con);
        SqlCommand cmd1 = new SqlCommand("SELECT Valoare FROM Valori", con);
        SqlDataReader sdr = cmd.ExecuteReader();
        int i = 0;
        while (sdr.Read())
        {
                x[i] = sdr.GetString(0);
                i++;
        }
        sdr.Close();
        SqlDataReader sdr1 = cmd1.ExecuteReader();
        i = 0;
        while(sdr1.Read())
        {
            y[i] = sdr1.GetString(0);
            i++;
        }
        sdr1.Close();
        i = 0;
        SqlCommand cmd2 = new SqlCommand("SELECT UM From Caracteristici", con);
        SqlDataReader sdr2 = cmd2.ExecuteReader();
        while (sdr2.Read())
        {
            z[i] = sdr2.GetString(0);
            i++;
        }
        sdr2.Close();
        for (i = 0; i <= 10; i++)
        {
            string[] RowData = { x[i], y[i], z[i] };
            dataGridView2.Rows.Add(RowData);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM