如何向Datagridview添加一列(已由数据源填充)

时间:2016-02-02 16:33:27

标签: c# winforms datagridview

我有2个datagridviews。我使用DB调用方法CellContentClick,其中包含其他datagridview。我必须在第二个datagridview中添加另一列并填充它。

我如何填充第二个datagridview:

//Connetion to Access DB 
const string strProvider =
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DataBase v1.3.accdb";

// Query to DB
var strSql = "SELECT * FROM [Проф прививки] WHERE id_ученика=" + dataGridView2.Rows[e.RowIndex].Cells[0].Value;

var con = new OleDbConnection(strProvider);
var cmd = new OleDbCommand(strSql, con);
cmd.CommandType = CommandType.Text;
var da = new OleDbDataAdapter(cmd);
var scores = new DataTable();
da.Fill(scores);
dataGridView3.DataSource = scores;

所以,它运作正常......

但是如果我按设计者向datagridview添加一个新列并填充数据,则该列不会被填充。

我如何填写新专栏:

for (var i = 0; i < dataGridView3.Rows.Count; i++)
{
    //Query to the other table
    strSql = "SELECT [Название прививки] FROM [Список прививок] WHERE [id_тип_прививки]=" +
    (int) dataGridView3.Rows[i].Cells[2].Value;

    //Use method GetFromDb which returns the selection
    dataGridView3.Rows[i].Cells[3].Value = GetFromDb(strSql, 0);
}

因此,该列为空,但如果我写MessageBox.Show(dataGridView3.Rows[0].Cells[3].Value.ToString()); 我会看到填充的数据。

如果我再次调用方法CellContentClick,我会看到该列已填满。

UPD: 完整代码:

public void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {try
        {
            const string strProvider =
                @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DataBase v1.3.accdb";
            var strSql = "SELECT * FROM [Проф прививки] WHERE id_ученика=" +
                         dataGridView2.Rows[e.RowIndex].Cells[0].Value;
            var con = new OleDbConnection(strProvider);
            var cmd = new OleDbCommand(strSql, con);
            cmd.CommandType = CommandType.Text;
            var da = new OleDbDataAdapter(cmd);
            var scores = new DataTable();
            da.Fill(scores);
            dataGridView3.DataSource = scores;

            //подтягиваем заболевания
            for (var i = 0; i < dataGridView3.Rows.Count; i++)
            {
                strSql = "SELECT [Название прививки] FROM [Список прививок] WHERE [id_тип_прививки]=" +
                         (int) dataGridView3.Rows[i].Cells[2].Value;
                dataGridView3.Rows[i].Cells[3].Value = GetFromDb(strSql, 0);
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(@"Ошибка! " + ex.Message);
        }

}

public string GetFromDb(string query, int num)
    {
        var result = "";
        var conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\DataBase v1.3.accdb";
        try
        {
            conn.Open();

            var cmd = new OleDbCommand(query, conn);
            var dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                result = dr.GetValue(num).ToString();

            }
            conn.Close();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message + @" : " + ex.StackTrace, @"Load Error");
        }

        return result;
    }

0 个答案:

没有答案