欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

DataGridView控件顯示行號(hào)的正確代碼及分析

 更新時(shí)間:2013年08月19日 09:17:07   作者:  
今天要用到DataGridView,想給它動(dòng)態(tài)的顯示行號(hào)。于是在網(wǎng)上找了一下解決方法。結(jié)果發(fā)現(xiàn)了不少問(wèn)題。然而就是這么一段有錯(cuò)的代碼,幾乎充斥著整個(gè)互聯(lián)網(wǎng),千篇一律的COPY,沒(méi)有一個(gè)人糾正

前些天在寫(xiě)個(gè)小程序,用到DataGridView,想給它動(dòng)態(tài)的顯示行號(hào)。不是很費(fèi)勁GOOGLE了一下,這GOOGLE不要緊,發(fā)現(xiàn)了不少問(wèn)題。以下基本上都是GOOGLE搜索出來(lái)的網(wǎng)上的一些解決方法,千篇一律都是這樣的:

復(fù)制代碼 代碼如下:

private void DataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {

            for (int i = 0; i < e.RowCount; i++)
            {
                this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
            }
            for (int i = e.RowIndex + e.RowCount; i < this.dgvKBRollUp.Rows.Count; i++)
            {
                this.dgvKBRollUp.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                this.dgvKBRollUp.Rows[i].HeaderCell.Value = (i + 1).ToString();
            }            
        }

private void DataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {

            for (int i = 0; i < e.RowCount; i++)
            {
                this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
            }
            for (int i = e.RowIndex + e.RowCount; i < this.dgvKBRollUp.Rows.Count; i++)
            {
                this.dgvKBRollUp.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                this.dgvKBRollUp.Rows[i].HeaderCell.Value = (i + 1).ToString();
            }            
        }

只要用過(guò)這段代碼的人就應(yīng)該發(fā)現(xiàn)這段代碼是運(yùn)行出錯(cuò)的。原因就出在RowsRemoved事件里,會(huì)拋出一個(gè)Index outof range的異常。然而就是這么一段有錯(cuò)的代碼,幾乎充斥著整個(gè)互聯(lián)網(wǎng),千篇一律的COPY,沒(méi)有一個(gè)人糾正。

先說(shuō)下這段代碼出錯(cuò)的原因吧:
在RowsRemoved事件里,最開(kāi)始生成DataGridView的數(shù)據(jù)的時(shí)候,也是會(huì)觸發(fā)這個(gè)事件的。這個(gè)時(shí)候DataGridView控件的Rows.Count就是0。那下面這行代碼就有問(wèn)題了:
復(fù)制代碼 代碼如下:

this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

e.RowIndex + i,這里對(duì)應(yīng)的是Rows[0],但是Rows.Count還是0啊,Rows[0]是不存在的。要存在Rows[0]起碼DataGridView控件要有一行才行。為了避免這個(gè)錯(cuò)誤,小小的修改代碼就行了:
復(fù)制代碼 代碼如下:

private void dgvKBRollUp_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            if (dgvKBRollUp.Rows.Count != 0)
            {
                for (int i = 0; i < e.RowCount; i++)
                {
                    this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                    this.dgvKBRollUp.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
                }

                for (int i = e.RowIndex + e.RowCount; i < this.dgvKBRollUp.Rows.Count; i++)
                {
                    this.dgvKBRollUp.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                    this.dgvKBRollUp.Rows[i].HeaderCell.Value = (i + 1).ToString();
                }

            }

只要加上一個(gè)對(duì)Rows.Count的判斷就可以避免這個(gè)錯(cuò)誤。希望網(wǎng)上的一些COPY的朋友也要注意了,以后COPY過(guò)來(lái)的時(shí)候,自己還是要?jiǎng)邮烛?yàn)證一下。將一個(gè)錯(cuò)誤的信息胡亂的傳播是對(duì)一些新手以及自己都不怎么好的。

最后附上微軟MSDN里面關(guān)于e.RowIndex和e.RowCount的一段代碼:

復(fù)制代碼 代碼如下:

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
            messageBoxCS.AppendFormat("{0} = {1}", "RowIndex", e.RowIndex);
            messageBoxCS.AppendLine();
            messageBoxCS.AppendFormat("{0} = {1}", "RowCount", e.RowCount);
            messageBoxCS.AppendLine();
            MessageBox.Show(messageBoxCS.ToString(), "RowsRemoved Event");

通過(guò)這段代碼你可以很輕松地跟蹤事件參數(shù)里的e.RowIndex和e.RowCount的值。當(dāng)然你可以DEBUG,一樣的。我就是DEBUG的O(∩_∩)O~

相關(guān)文章

最新評(píng)論