简体   繁体   English

C# WinForm DataGridView 添加未绑定的分栏符 CellFormatting

[英]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.我有几个 DataGridViews,它们使用 CellFormatting 事件处理程序将数据库 ID 值转换为用户友好的显示字符串。 However, one of these DataGridViews has an added DataGridViewButtonColumn to allow the user to perform some data analysis on the row data.但是,其中一个 DataGridViews 添加了 DataGridViewButtonColumn 以允许用户对行数据执行一些数据分析。 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 (问题:如果我在运行时将 Button 列添加到 DataGridView,网格将显示原始数据库 ID 值而不是用户友好的字符串 ( 见图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:编辑:这是 CellFormatting 代码:

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()?虽然我同意发布 CellFormatting() 代码开始会有所帮助,但我仍然认为这是一个完全合理的问题/问题:为什么在世界上添加一个未绑定的列会破坏 CellFormatting() 中非常简单的逻辑?

Copy the code that creates the DataGridView at Design-Time from the InitializeComponent()InitializeComponent()复制在设计时创建 DataGridView 的代码

Create the DataGridView's columns at Run-Time in the Form_Load() code.Form_Load()代码中在运行时创建 DataGridView 的列。

The columns indexes are obviously overlapping and its easier to solve it in one place and at Run-Time gives you more control.列索引显然是重叠的,并且更容易在一个地方解决它,并且在运行时为您提供更多控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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