简体   繁体   中英

C# Insert value into DataGridView

I have a problem with a DataGridView , as you can see from down here, when I enter a value ( LOL ) within the cells is actually only it appears in the last line

的DataGridView

Code:

private void CaricaNoteLavaggio()
{
    using (DatabaseConnection db = new DatabaseConnection())
    {
        const string query = "" // My query
        using (MySqlDataReader reader = db.ExecuteReader(query))
        {
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    DataGridViewRow row = new DataGridViewRow();
                    dataGridNoteLavaggio.Rows.Add(row);

                    foreach (DataGridViewCell cell in dataGridNoteLavaggio.Rows[dataGridNoteLavaggio.NewRowIndex].Cells)
                    {
                        cell.Value = "LOL";
                    }
                }
            }
        }
    }
}

The columns of the DataGridView I create dynamically using this piece of code (if that can be useful)

        DataGridViewTextBoxColumn txtId = new DataGridViewTextBoxColumn();
        txtId.HeaderText = "ID";
        txtId.Name = "id";
        txtId.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        txtId.Visible = false;

        dataGridNoteLavaggio.Columns.Add(txtId);

        // Function that returns the list of languages (EN, FR, ES, etc. ...)
        List<string> lingue = DatabaseFunctions.GetLingue();
        foreach (var lingua in lingue)
        {
            DataGridViewTextBoxColumn txtDesc = new DataGridViewTextBoxColumn();
            txtDesc.HeaderText = lingua;
            txtDesc.Name = lingua;
            txtDesc.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

            dataGridNoteLavaggio.Columns.Add(txtDesc);
        }

I tried it with debugging to follow the indices in the cycle are correct but despite the result is wrong.

Because you insert data in the new row and new row always added to last of all.

for change data from first row after adding row you must change this code:

dataGridNoteLavaggio.Rows[dataGridNoteLavaggio.NewRowIndex].Cells

to

dataGridNoteLavaggio.Rows[index].Cells

and index set from Rows.Add() method. and your codes should like this:

private void CaricaNoteLavaggio()
{
    using (DatabaseConnection db = new DatabaseConnection())
    {
        const string query = "" // My query
        using (MySqlDataReader reader = db.ExecuteReader(query))
        {
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    DataGridViewRow row = new DataGridViewRow();
                    var index = dataGridNoteLavaggio.Rows.Add(row);

                    foreach (DataGridViewCell cell in dataGridNoteLavaggio.Rows[index].Cells)
                    {
                        cell.Value = "LOL";
                    }
                }
            }
        }
    }
}

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