简体   繁体   中英

C# WinForm DataGridView Adding Unbound Column Breaks CellFormatting

I have several DataGridViews that use the CellFormatting event handler to convert database ID values into user-friendly display strings. However, one of these DataGridViews has an added DataGridViewButtonColumn to allow the user to perform some data analysis on the row data. The problem: if I add the Button column to the DataGridView at run-time, the grid displays the raw database ID value rather than the user-friendly string ( 见图1 ). If I comment out the logic that adds the unbound column, the grid displays the user-friendly string ( 见图2 ).

EDIT: Here is the CellFormatting code:

private void GridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
   if (e.ColumnIndex == 1 && e.RowIndex >= 0 && e.Value != null)
   {
       int    experimentId = Convert.ToInt32(e.Value);
       string displayValue = ConvertToExperiment(experimentId);

       e.Value             = displayValue;
       e.FormattingApplied = true;
   }
}

and here is the initialization code that adds the button column:

private void LoadExperimentHistory()
{
    string selectCommand = "SELECT * FROM ExperimentLog";

    SQLiteCommand     sqlCommand     = new SQLiteCommand(selectCommand, databaseConnection);
    SQLiteDataAdapter sqlDataAdapter = new SQLiteDataAdapter(sqlCommand);

    DataSet dataSet = new DataSet();

    try
    {
        sqlDataAdapter.Fill(dataSet);
        historyDataTable = dataSet.Tables[0];

        if (historyDataTable != null)
        {
            dataGridView.DataSource = historyDataTable;
            dataGridView.Columns["ID"          ].Visible    = false;
            dataGridView.Columns["ExperimentId"].HeaderText = "Experiment";
            dataGridView.Columns["StartDate"   ].HeaderText = "Start Date";

            if (dataGridView.Columns["Analysis"] == null)
            {
                DataGridViewButtonColumn analysisColumn = new DataGridViewButtonColumn
                {
                    Name                        = "Analysis",
                    Text                        = "Run Analysis",
                    UseColumnTextForButtonValue = true,
                    FlatStyle                   = FlatStyle.Standard
                };

                analysisColumn.DefaultCellStyle.BackColor = Color.LightGray;
                dataGridView.Columns.Add(analysisColumn);
            }

        }
    }
    catch (Exception exception)
    {
        MessageForm messageForm = new MessageForm(exception.Message, MessageBoxButtons.OK);
        messageForm.ShowDialog();
    }
}

SECOND EDIT:

While I agree that it would have been helpful to post the CellFormatting() code to start, I still think that this is a completely reasonable question/problem: why in the world does adding an unbound column break the very simple logic in CellFormatting()?

Copy the code that creates the DataGridView at Design-Time from the InitializeComponent()

Create the DataGridView's columns at Run-Time in the Form_Load() code.

The columns indexes are obviously overlapping and its easier to solve it in one place and at Run-Time gives you more control.

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