首先
单选按钮中添加方法
private void rbtnCDZ_CheckedChanged(object sender, EventArgs e)
{
rbtnCheckedInfo();
}
private void rbtnYCM_CheckedChanged(object sender, EventArgs e)
{
rbtnCheckedInfo();
}
定义排序方式 单选一种 执行 按 条件 排序
/// <summary>
/// 重新 排序 DGV_CDXXLB
/// </summary>
public void rbtnCheckedInfo()
{
if (rbtnCDZ.Checked)
{
DGV_CDXXLB.Sort(new RowComparer(SortOrder.Ascending, "充电中"));
}
else if (rbtnYCM.Checked)
{
DGV_CDXXLB.Sort(new RowComparer(SortOrder.Ascending, "已充满"));
}
else if (rbtnCDGZ.Checked)
{
DGV_CDXXLB.Sort(new RowComparer(SortOrder.Ascending, "故障"));
}
else if (rbtnCDQ.Checked)
{
//DGV_CDXXLB.Sort(new RowComparer(SortOrder.Ascending, 5));
}
}
datagridview 是动态添加 也就是用的 Add
private void button7_Click(object sender, EventArgs e)
{
DataGridViewRow dr = new DataGridViewRow();
foreach (DataGridViewColumn dc in DGV_CDXXLB.Columns)
{
dr.Cells.Add(dc.CellTemplate.Clone() as DataGridViewCell);//给行添加单元格
}
dr.Cells[0].Value = "1";
dr.Cells[1].Value = "充满";
dr.Cells[2].Value = "充满";
dr.Cells[3].Value = "2";
dr.Cells[4].Value = "11.BAT_SITE";
dr.Cells[5].Value = "11.BAT_TYPE";
dr.Cells[6].Value = "11.RONG";
dr.Cells[7].Value = "11.BAT_VOL1";
DGV_CDXXLB.Rows.Add(dr);
}
在添加行的时候会执行 DGV_CDXXLB_RowsAdded :
private void DGV_CDXXLB_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
rbtnCheckedInfo();
}
关键代码: 重写排序方式 ,用的System.Collections.IComparer
#region 重写dgv排序
// DGV_CDXXLB.Sort(new RowComparer(SortOrder.Ascending,1));
private class RowComparer : System.Collections.IComparer
{
private static int sortOrderModifier = 1;
string _pxtype = string.Empty;
/// <summary>
/// dgv排序
/// </summary>
/// <param name="sortOrder">排序方式</param>
/// <param name="cellindex">dgv行rows.index</param>
public RowComparer(SortOrder sortOrder, string pxtype)
{
_pxtype = pxtype;
if (sortOrder == SortOrder.Descending)
{
sortOrderModifier = -1;
}
else if (sortOrder == SortOrder.Ascending)
{
sortOrderModifier = 1;
}
}
public int Compare(object x, object y)
{
return Compare(x, y, _pxtype);
}
public int Compare(object x, object y, string pxtype)
{
DataGridViewRow DataGridViewRow1 = (DataGridViewRow)x;
DataGridViewRow DataGridViewRow2 = (DataGridViewRow)y;
int CompareResult = 0;
if (DataGridViewRow1.Cells[1].Value.ToString() == _pxtype && DataGridViewRow2.Cells
[1].Value.ToString() != pxtype)
{
CompareResult = -1;
}
if (DataGridViewRow1.Cells[1].Value.ToString() != pxtype && DataGridViewRow2.Cells
[1].Value.ToString() == pxtype)
{
CompareResult = 1;
}
if (CompareResult == 0)
{
CompareResult = System.String.Compare(
DataGridViewRow1.Cells[0].Value.ToString(),
DataGridViewRow2.Cells[0].Value.ToString());
}
return CompareResult * sortOrderModifier;
}
}
#endregion
单选按钮中添加方法
private void rbtnCDZ_CheckedChanged(object sender, EventArgs e)
{
rbtnCheckedInfo();
}
private void rbtnYCM_CheckedChanged(object sender, EventArgs e)
{
rbtnCheckedInfo();
}
定义排序方式 单选一种 执行 按 条件 排序
/// <summary>
/// 重新 排序 DGV_CDXXLB
/// </summary>
public void rbtnCheckedInfo()
{
if (rbtnCDZ.Checked)
{
DGV_CDXXLB.Sort(new RowComparer(SortOrder.Ascending, "充电中"));
}
else if (rbtnYCM.Checked)
{
DGV_CDXXLB.Sort(new RowComparer(SortOrder.Ascending, "已充满"));
}
else if (rbtnCDGZ.Checked)
{
DGV_CDXXLB.Sort(new RowComparer(SortOrder.Ascending, "故障"));
}
else if (rbtnCDQ.Checked)
{
//DGV_CDXXLB.Sort(new RowComparer(SortOrder.Ascending, 5));
}
}
datagridview 是动态添加 也就是用的 Add
private void button7_Click(object sender, EventArgs e)
{
DataGridViewRow dr = new DataGridViewRow();
foreach (DataGridViewColumn dc in DGV_CDXXLB.Columns)
{
dr.Cells.Add(dc.CellTemplate.Clone() as DataGridViewCell);//给行添加单元格
}
dr.Cells[0].Value = "1";
dr.Cells[1].Value = "充满";
dr.Cells[2].Value = "充满";
dr.Cells[3].Value = "2";
dr.Cells[4].Value = "11.BAT_SITE";
dr.Cells[5].Value = "11.BAT_TYPE";
dr.Cells[6].Value = "11.RONG";
dr.Cells[7].Value = "11.BAT_VOL1";
DGV_CDXXLB.Rows.Add(dr);
}
在添加行的时候会执行 DGV_CDXXLB_RowsAdded :
private void DGV_CDXXLB_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
rbtnCheckedInfo();
}
关键代码: 重写排序方式 ,用的System.Collections.IComparer
#region 重写dgv排序
// DGV_CDXXLB.Sort(new RowComparer(SortOrder.Ascending,1));
private class RowComparer : System.Collections.IComparer
{
private static int sortOrderModifier = 1;
string _pxtype = string.Empty;
/// <summary>
/// dgv排序
/// </summary>
/// <param name="sortOrder">排序方式</param>
/// <param name="cellindex">dgv行rows.index</param>
public RowComparer(SortOrder sortOrder, string pxtype)
{
_pxtype = pxtype;
if (sortOrder == SortOrder.Descending)
{
sortOrderModifier = -1;
}
else if (sortOrder == SortOrder.Ascending)
{
sortOrderModifier = 1;
}
}
public int Compare(object x, object y)
{
return Compare(x, y, _pxtype);
}
public int Compare(object x, object y, string pxtype)
{
DataGridViewRow DataGridViewRow1 = (DataGridViewRow)x;
DataGridViewRow DataGridViewRow2 = (DataGridViewRow)y;
int CompareResult = 0;
if (DataGridViewRow1.Cells[1].Value.ToString() == _pxtype && DataGridViewRow2.Cells
[1].Value.ToString() != pxtype)
{
CompareResult = -1;
}
if (DataGridViewRow1.Cells[1].Value.ToString() != pxtype && DataGridViewRow2.Cells
[1].Value.ToString() == pxtype)
{
CompareResult = 1;
}
if (CompareResult == 0)
{
CompareResult = System.String.Compare(
DataGridViewRow1.Cells[0].Value.ToString(),
DataGridViewRow2.Cells[0].Value.ToString());
}
return CompareResult * sortOrderModifier;
}
}
#endregion
其中pxtype 是排序条件.