如何选择要将数据添加到DataGridView中的列?

时间:2016-03-29 13:41:52

标签: c# database datagridview

我有这个源代码:

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

    }

我有一个问题:在我完成插入RowData后,如何选择移动到另一列?因为使用此代码,所有数据仅在一个列上。

3 个答案:

答案 0 :(得分:1)

问题在于,当您将数据添加到行时,您将添加如下:

String filename = "C:\\Documents\\Airtel Youthstar-Tutorial.mp4";   
    IContainer container = IContainer.make();  
    int result = container.open(filename, IContainer.Type.READ, null);  
    long duration = container.getDuration();  
    System.out.println("Duration (ms): " + duration);  

这始终是将数据添加到第一列。要将数据添加到其他列,您需要将数据添加到您需要的列索引中,例如

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

如果有3列,则会将数据添加到第二列。

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

答案 1 :(得分:0)

这是一个想法,我希望你明白! 此示例将添加一行,其中填充了所有列:

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;
}

string[] row = new string[] { item.Id, item.Name, item.Description };的顺序应该等于您的datagridView列顺序。

我的代码中的日志是List<Log>,但应该适用于任何其他集合类型。

如果日志中填充了各种对象,则此示例将添加相同数量的行。

答案 2 :(得分:0)

我解决了这个问题。我会在这里留下解决方案:

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