【问题标题】:How to color selected rows in GridView in c#如何在 C# 中为 GridView 中的选定行着色
【发布时间】:2013-10-02 09:02:29
【问题描述】:

我在 gridview 中选择所有带有后续代码的行

gridView1.SelectAll();

现在我想在 gridview 中为选定的行着色,例如蓝色。

我该怎么做?

【问题讨论】:

    标签: c# winforms gridview devexpress xtragrid


    【解决方案1】:

    我相信你说的是DevExpress XtraGrid。如果是这样,那么根据您的特定任务,有多种方法可以突出显示 DevExpress XtraGrid 中的特定行。 例如,要突出显示任何选定的行,您可以使用以下代码:

    gridView1.Appearance.SelectedRow.BackColor = Color.Red;
    

    要使用自定义条件突出显示特定行,您可以使用 GridView.RowStyle 事件:

    void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) {
        if((e.State & DevExpress.XtraGrid.Views.Base.GridRowCellState.Selected) != 0) {
            // check some conditions
            e.HighPriority = true;
            e.Appearance.BackColor = Color.Blue;
        }
    }
    

    请在以下帮助文章中阅读有关所有这些方法的更多信息:Customizing Appearances of Individual Rows and Cells

    【讨论】:

      【解决方案2】:
      foreach (DataGridViewRow row in dataGridView1.SelectedRows)
      {
          row.DefaultCellStyle.BackColor = Color.Blue;
      }
      

      【讨论】:

      • 我使用 DevExpress GridView
      • 哎呀。你没有提到。 :)
      【解决方案3】:

      您可以使用 SelectedRowStyle 属性

      MSDN exemple

      【讨论】:

        【解决方案4】:

        试试这个,

        To change selected rows

        private void button1_Click(object sender, EventArgs e)
            {
                foreach (DataGridViewRow item in dataGridView1.SelectedRows)
                {
                    if (null != item)
                    {
                        item.DefaultCellStyle.BackColor = Color.Blue;
                    }
                }            
            }
        

        To change all rows

        foreach (DataGridViewRow item in dataGridView1.Rows)
        {
        }
        

        【讨论】:

          【解决方案5】:

          对于gridview,为事件CellFormatting设置下面的方法

          private void StocksDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) {
              StocksDataGridView.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.OrangeRed;
          }
          

          【讨论】:

            【解决方案6】:

            你说你使用了DevexpressGrid。因此,可以通过在设计时更改 girdView's properties 轻松实现。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-04-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多