number007cool 2021-09-06 11:13 采纳率: 20%
浏览 38

C# 设置datagrid的背景色为黄色无效 ,还是控件默认的颜色



```c#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mqtt
{
    public class lineType
    {
        public string Name{get; set;}
        public string Value{get; set;}
        public string Unit{get; set;}

        public lineType(string name, string value)
        {
            this.Name = name;
            this.Value = value;
            this.Unit = "";
        }
        public lineType()
        {

        }

       
    }

    public partial class Frm_Info : Form
    {
        public List<lineType> mShowList = new List<lineType>();

        public Frm_Info()
        {
            InitializeComponent();

            Timer tm = new Timer();
            tm.Interval = 100;
            tm.Tick += tm_Tick;
            tm.Start();

            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.TopLevel = true;

            mShowList.Add(new lineType("w1", "100") );
            mShowList.Add(new lineType("w2", "110"));
            mShowList.Add(new lineType("w3", "120"));
            mShowList.Add(new lineType("w4", "130"));

            InitDgv(dgv1);
        }

        private void InitDgv(DataGridView dgv)
        {
            dgv.DataSource = mShowList;

            dgv.BackgroundColor = Color.Yellow;
            dgv.ForeColor = Color.Red;
            dgv.Columns[0].Width = 50;
            dgv.Columns[1].Width = 120;
            dgv.Columns[dgv.ColumnCount - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            this.Height = dgv.Rows[0].Height * ( mShowList.Count() + 1 ) ;

            for(int i = 0; i< dgv.Rows.Count; i++)
            {
                dgv.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.DarkGreen;
                dgv.Rows[i].DefaultCellStyle.ForeColor = Color.Red;
            }

            //dgv.Refresh();
        }

        void tm_Tick(object sender, EventArgs e)
        {
            Point screenPoint = Control.MousePosition;//鼠标相对于屏幕左上角的坐标

            Point formPoint = this.PointToClient(Control.MousePosition);//鼠标相对于窗体左上角的坐标

            Console.WriteLine("x " + screenPoint.X + "y " + screenPoint.Y);

            this.Location = new Point(screenPoint.X + 10, screenPoint.Y + 10);
        }
    }
}




```

  • 写回答

2条回答 默认 最新

  • number007cool 2021-09-06 11:18
    关注

    img

    效果如上,希望真个dgv是黄色背景,但是还是默认的。 不知为何

    评论

报告相同问题?

问题事件

  • 创建了问题 9月6日

悬赏问题

  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析