日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

datagridview选中获取行号_DataGridView控件显示行号的正确代码及分析

發布時間:2024/9/27 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 datagridview选中获取行号_DataGridView控件显示行号的正确代码及分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前些天在寫個小程序,用到DataGridView,想給它動態的顯示行號。不是很費勁GOOGLE了一下,這GOOGLE不要緊,發現了不少問題。以下基本上都是GOOGLE搜索出來的網上的一些解決方法,千篇一律都是這樣的:

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();

}

}

只要用過這段代碼的人就應該發現這段代碼是運行出錯的。原因就出在RowsRemoved事件里,會拋出一個Index outof range的異常。然而就是這么一段有錯的代碼,幾乎充斥著整個互聯網,千篇一律的COPY,沒有一個人糾正。

先說下這段代碼出錯的原因吧:在RowsRemoved事件里,最開始生成DataGridView的數據的時候,也是會觸發這個事件的。這個時候DataGridView控件的Rows.Count就是0。那下面這行代碼就有問題了:

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

e.RowIndex + i,這里對應的是Rows[0],但是Rows.Count還是0啊,Rows[0]是不存在的。要存在Rows[0]起碼DataGridView控件要有一行才行。為了避免這個錯誤,小小的修改代碼就行了:

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();

}

}

只要加上一個對Rows.Count的判斷就可以避免這個錯誤。希望網上的一些COPY的朋友也要注意了,以后COPY過來的時候,自己還是要動手驗證一下。將一個錯誤的信息胡亂的傳播是對一些新手以及自己都不怎么好的。

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

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");

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

總結

以上是生活随笔為你收集整理的datagridview选中获取行号_DataGridView控件显示行号的正确代码及分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。