如何防止Datagridview在打开时选择第一个单元格 [英] How to prevent Datagridview from selecting the first cell on opening

查看:86
本文介绍了如何防止Datagridview在打开时选择第一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找到一种方法来防止我的datagridview选择第一个单元格作为默认单元格。现在我有代码,如果负数在导入的单元格中,我的datagridview中的单元格的背景颜色变为红色。但是,这在我的第一个单元格中无法正常工作,因为它在导入时默认已突出显示。如果有人能够找到如何关闭单元格的选择,我将非常感激! :)



我知道它一定很像 DataGridView1.CurrentCell.Selected = False

解决方案

就是这么简单:)



datagridview1.ClearSelection()


< blockquote>我按照你的描述编写了一个测试,并且所有单元格都正确着色。

有更多信息在 http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcell.aspx [ ^ ]



  public   partial   class  Form1:表格
{
private Random rnd;
private int nSize = 4 ;

public Form1()
{
InitializeComponent();
rnd = new Random();
for int c = 0 ; c < nSize; c ++)
{
this .dataGridView1 .Columns.Add( Col + c.ToString(), Col + c.ToString());
}
}

私有 void buttonImport_Click( object sender,EventArgs e)
{
this .dataGridView1.Rows.Clear ();
int n = this .dataGridView1.Columns.Count;
string [] nums;
for int r = 0 ; r < nSize; r ++)
{
nums = new string [n];
for int c = 0 ; c < n; c ++)
{
nums [c] =(rnd.Next(-10, 10 ))的ToString();
}
.dataGridView1.Rows.Add(nums);

}
ColorCells();
}

私有 void ColorCells()
{
int n = .dataGridView1.Columns.Count;
for int r = 0 ; r < nSize; r ++)
{
for (< span class =code-keyword> int
c = 0 ; c < n ; c ++)
{
string val =( string .dataGridView1.Rows [R] .Cells并[c]。价值;
int v;
if Int32 .TryParse(val, out v)== false continue ;

if (v< 0)
this .dataGridView1。行[r]。细胞[c] .Style.BackColor = Color.Red;
else
this .dataGridView1.Rows [r] .Cells [E] .Style.BackColor = Color.White;
}

}
}
}


I'm trying to find a way to prevent my datagridview from selecting the first cell as default cell. Right now I have code that turns the backcolor of the cells in my datagridview to red if negative numbers are in the cells on import. However this won't work properly in my first cell since its already highlighted by default on import. If anyone can find out how to turn the selecting of the cell off I would greatly appreciate it! :)

I know it must be something simple like DataGridView1.CurrentCell.Selected = False

解决方案

It is just that simple :)

datagridview1.ClearSelection()


I wrote a test following you're description and all cells colored correctly.
There's more info at http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcell.aspx[^]

public partial class Form1 : Form
    {
        private Random rnd;
        private int nSize = 4;

        public Form1()
        {
            InitializeComponent();
            rnd = new Random();
            for (int c = 0; c < nSize; c++)
            {
                this.dataGridView1.Columns.Add("Col" + c.ToString(), "Col" + c.ToString());
            }
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Clear();
            int n = this.dataGridView1.Columns.Count;
            string[] nums;
            for (int r = 0; r < nSize; r++)
            {
                nums = new string[n];
                for (int c = 0; c < n; c++)
                {
                    nums[c] = (rnd.Next(-10, 10)).ToString();
                }
                this.dataGridView1.Rows.Add(nums);
                
            }
            ColorCells();
        }

        private void ColorCells()
        {
            int n = this.dataGridView1.Columns.Count;
            for (int r = 0; r < nSize; r++)
            {
                for (int c = 0; c < n; c++)
                {
                    string val = (string) this.dataGridView1.Rows[r].Cells[c].Value;
                    int v;
                    if(Int32.TryParse(val, out v) == false) continue;

                    if(v<0)
                        this.dataGridView1.Rows[r].Cells[c].Style.BackColor = Color.Red;
                    else
                        this.dataGridView1.Rows[r].Cells[c].Style.BackColor = Color.White;
                }
                
            }
        }
    }


这篇关于如何防止Datagridview在打开时选择第一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆