c#datagridview按行背景颜色排序 [英] c# datagridview sorting by row background color
问题描述
大家好,
我在Windows窗体上有一个带有三列的datagridview.每行在某种程度上用红色,绿色和黄色上色.
现在我想要的是,当我单击列标题时,我想根据颜色对它们进行排序.就像按升序排列一样,网格将先显示绿色行,然后显示黄色行,然后显示红色行,并按降序显示相反的行.
怎么可能?
等待您的回应.
谢谢
Kapil
另一种方法是在数据库中为颜色代码创建一个字段,例如ColorCode
,并指定值1-绿色,2-黄色,3 - 红色的.
然后基于此字段,将颜色应用于CellFormatting
事件中DataGridView
的行.
私有 无效 dataGridView1_CellFormatting(对象发件人,DataGridViewCellFormattingEventArgs e) { 如果(例如,RowIndex < 0 || dataGridView1.Rows [e.RowIndex] .Cells [" ].Value == DBNull.Value) 返回; int colorCode =( int )dataGridView1.Rows [e.RowIndex] .Cells [ ColorCode"].Value; e.CellStyle.BackColor = colorCode == 1 吗?颜色.绿色: colorCode == 2 吗?颜色.黄色:颜色.红色; }
然后单击ColorCode
列的列标题,以根据ColorCode
对行进行排序.列不可见.可以使用以下选项之一,使用DataGridView
绑定到的bindingSource1
对隐藏的列进行排序.
选项1 .
使用例如Button1
和Button2
的按钮分别对隐藏的列进行升序和降序排序.然后在按钮的Click
事件中,如下设置bindingSource1
的Sort
属性
// Button1点击事件 bindingSource1.Sort = " ; // Button2点击事件 bindingSource1.Sort = " ;
Option2
默认情况下,当单击该列的标题时,DataGridView
会按升序和降序对列进行排序.要禁用此功能,可以将Column
的SortMode
属性设置为NotSortable
,然后可以处理ColumnHeaderMouseClick
以便对ColorCode
列进行排序
dataGridView1.Columns [ 0 ].SortMode = DataGridViewColumnSortMode.NotSortable; dataGridView1.Columns [ 1 ].SortMode = DataGridViewColumnSortMode.NotSortable; 私有 无效 dataGridView1_ColumnHeaderMouseClick(对象发件人, DataGridViewCellMouseEventArgs e){ // 如果单击第一列标题,则按升序对ColorCode进行排序 如果(例如,ColumnIndex == 0 ) bindingSource1.Sort = " ; // 如果单击第二列标题,则按降序对ColorCode进行排序 其他 如果(例如,ColumnIndex == 1 ) bindingSource1.Sort = " ; }
Hello All,
I have a datagridview with three columns on the windows form. Each row is colored on some basis with Red, Green and Yellow colors.
Now what I want is when I click on column headers I want to sort them according to colors. Like in ascending order the grid will show the Green rows first then Yellow and then the Red and in descending order the opposite.
How it is possible?
Awaiting your response.
Thanks
Kapil
An alternative is to create one field in the database for color code, sayColorCode
, assign the values say 1 - Green, 2 - Yellow, 3 - Red.
Then based on this field, apply color to the rows of theDataGridView
inCellFormatting
event.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.RowIndex < 0 || dataGridView1.Rows[e.RowIndex] .Cells["ColorCode"].Value==DBNull.Value) return; int colorCode = (int)dataGridView1.Rows[e.RowIndex].Cells["ColorCode"].Value; e.CellStyle.BackColor = colorCode == 1 ? Color.Green : colorCode == 2 ? Color.Yellow : Color.Red; }
Then click on the column header ofColorCode
column, to sort the rows according to theColorCode
.
You cannot sort the hidden column visually by clicking on theDataGridView
as the column is not visible. One of the following options can be used, to sort the hidden column, using thebindingSource1
to which theDataGridView
is bound.
Option1.
Use buttons, sayButton1
andButton2
to sort the hidden column in ascending and descending order respectively. Then in theClick
event of buttons set theSort
property ofbindingSource1
as follows
//Button1 click event bindingSource1.Sort = "ColorCode"; //Button2 click event bindingSource1.Sort = "ColorCode DESC";
Option2
By default theDataGridView
sorts the column in ascending and descending order alternatively, when the header of that column is clicked. To disable this theSortMode
property ofColumn
can be set toNotSortable
and then theColumnHeaderMouseClick
can be handled to sort theColorCode
column
dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable; dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable; private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { //If first column header is clicked sort ColorCode in ascending order if (e.ColumnIndex==0) bindingSource1.Sort = "ColorCode"; //If second column header is clicked sort ColorCode in descending order else if (e.ColumnIndex==1) bindingSource1.Sort = "ColorCode DESC"; }
这篇关于c#datagridview按行背景颜色排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!