{
i++;
string[] row0 ={ i.ToString(),tLable.intoLable01,intoLable02};
DataGridViewRowCollection drow = this.dataGridView1.Rows;
drow.Add(row0);
} 使用DataGridViewRowCollection 加入新行,但是怎么才能把加入的新行排到DataGridView的第一行?使用排序可以吗?怎么才能设置按照那一行排序?
i++;
string[] row0 ={ i.ToString(),tLable.intoLable01,intoLable02};
DataGridViewRowCollection drow = this.dataGridView1.Rows;
drow.Add(row0);
} 使用DataGridViewRowCollection 加入新行,但是怎么才能把加入的新行排到DataGridView的第一行?使用排序可以吗?怎么才能设置按照那一行排序?
解决方案 »
- 怎么老提示字符格式有问题呢?大家帮忙给看看
- 高分求 Enterprise Library January 2006 下连接oracle的配置文件
- 关于c#操作excel图表的中英文版本问题
- Win2003Server 杀Excel进程。。。。。。。。高手进
- 谁有把C#代码转化为vb.net的工具?找到就给分
- 怎样在字符串中查找一个字符串?????
- 与 VB 中的“On Error Resume Next”相同的是???
- vs .net 2003 里面,大纲里面怎么把自动生成的代码折叠起来?
- 如何进行窗口间值传递?
- 请教:windows.net下如何用vs.net下的web应用程序??
- 高分求救!!!在DataGridView中怎样实现数据行的上移和下移,以及数据的插入
- 求一个问题~急。。。。
drow.Add(row0);with
drow.Insert(0,row0);
1、以编程方式进行排序
具体为使用 SortOrder 和 SortedColumn 属性确定排序的方向,并使用 SortGlyphDirection 属性手动设置排序的标志符号。Sort 方法的 Sort(DataGridViewColumn,ListSortDirection) 重载仅用于在单列中对数据进行排序。using System;
using System.ComponentModel;
using System.Windows.Forms;class Form1 : Form
{
private Button sortButton = new Button();
private DataGridView dataGridView1 = new DataGridView(); // Initializes the form.
// You can replace this code with designer-generated code.
public Form1()
{
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.SelectionMode =
DataGridViewSelectionMode.ColumnHeaderSelect;
dataGridView1.MultiSelect = false; sortButton.Dock = DockStyle.Bottom;
sortButton.Text = "Sort"; Controls.Add(dataGridView1);
Controls.Add(sortButton);
Text = "DataGridView programmatic sort demo";
} // Establishes the main entry point for the application.
[STAThreadAttribute()]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
} // Populates the DataGridView.
// Replace this with your own code to populate the DataGridView.
public void PopulateDataGridView()
{
// Add columns to the DataGridView.
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].HeaderText = "Last Name";
dataGridView1.Columns[1].HeaderText = "City"; // Populate the DataGridView.
dataGridView1.Rows.Add(new string[] { "Parker", "Seattle" });
dataGridView1.Rows.Add(new string[] { "Watson", "Seattle" });
dataGridView1.Rows.Add(new string[] { "Osborn", "New York" });
dataGridView1.Rows.Add(new string[] { "Jameson", "New York" });
dataGridView1.Rows.Add(new string[] { "Brock", "New Jersey" });
} protected override void OnLoad(EventArgs e)
{
sortButton.Click += new EventHandler(sortButton_Click); PopulateDataGridView();
base.OnLoad(e);
} private void sortButton_Click(object sender, System.EventArgs e)
{
// Check which column is selected, otherwise set NewColumn to null.
DataGridViewColumn newColumn =
dataGridView1.Columns.GetColumnCount(
DataGridViewElementStates.Selected) == 1 ?
dataGridView1.SelectedColumns[0] : null; DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
ListSortDirection direction; // If oldColumn is null, then the DataGridView is not currently sorted.
if (oldColumn != null)
{
// Sort the same column again, reversing the SortOrder.
if (oldColumn == newColumn &&
dataGridView1.SortOrder == SortOrder.Ascending)
{
direction = ListSortDirection.Descending;
}
else
{
// Sort a new column and remove the old SortGlyph.
direction = ListSortDirection.Ascending;
oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
}
}
else
{
direction = ListSortDirection.Ascending;
} // If no column has been selected, display an error dialog box.
if (newColumn == null)
{
MessageBox.Show("Select a single column and try again.",
"Error: Invalid Selection", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
dataGridView1.Sort(newColumn, direction);
newColumn.HeaderCell.SortGlyphDirection =
direction == ListSortDirection.Ascending ?
SortOrder.Ascending : SortOrder.Descending;
}
}
}
对选择的 DataGridViewColumn 进行排序,如果列中有重复值,则使用 ID 列确定最终顺序。#region Using directivesusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;#endregion
class Form1 : Form
{
private DataGridView dataGridView1 = new DataGridView(); // Establish the main entry point for the application.
[STAThreadAttribute()]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
} public Form1()
{
// Initialize the form.
// This code can be replaced with designer generated code.
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.SortCompare += new DataGridViewSortCompareEventHandler(
this.dataGridView1_SortCompare);
Controls.Add(this.dataGridView1);
this.Text = "DataGridView.SortCompare demo"; PopulateDataGridView();
} // Replace this with your own population code.
public void PopulateDataGridView()
{
// Add columns to the DataGridView.
dataGridView1.ColumnCount = 3; // Set the properties of the DataGridView columns.
dataGridView1.Columns[0].Name = "ID";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[2].Name = "City";
dataGridView1.Columns["ID"].HeaderText = "ID";
dataGridView1.Columns["Name"].HeaderText = "Name";
dataGridView1.Columns["City"].HeaderText = "City"; // Add rows of data to the DataGridView.
dataGridView1.Rows.Add(new string[] { "1", "Parker", "Seattle" });
dataGridView1.Rows.Add(new string[] { "2", "Parker", "New York" });
dataGridView1.Rows.Add(new string[] { "3", "Watson", "Seattle" });
dataGridView1.Rows.Add(new string[] { "4", "Jameson", "New Jersey" });
dataGridView1.Rows.Add(new string[] { "5", "Brock", "New York" });
dataGridView1.Rows.Add(new string[] { "6", "Conner", "Portland" }); // Autosize the columns.
dataGridView1.AutoResizeColumns();
} private void dataGridView1_SortCompare(object sender,
DataGridViewSortCompareEventArgs e)
{
// Try to sort based on the cells in the current column.
e.SortResult = System.String.Compare(
e.CellValue1.ToString(), e.CellValue2.ToString()); // If the cells are equal, sort based on the ID column.
if (e.SortResult == 0 && e.Column.Name != "ID")
{
e.SortResult = System.String.Compare(
dataGridView1.Rows[e.RowIndex1].Cells["ID"].Value.ToString(),
dataGridView1.Rows[e.RowIndex2].Cells["ID"].Value.ToString());
}
e.Handled = true;
}
}
使用 Sort 方法的 Sort(IComparer) 重载自定义排序,该重载采用 IComparer 接口的实现来执行多列排序。#region Using directivesusing System;
using System.Drawing;
using System.Windows.Forms;#endregionclass Form1 : Form
{
private DataGridView DataGridView1 = new DataGridView();
private FlowLayoutPanel FlowLayoutPanel1 = new FlowLayoutPanel();
private Button Button1 = new Button();
private RadioButton RadioButton1 = new RadioButton();
private RadioButton RadioButton2 = new RadioButton(); // Establish the main entry point for the application.
[STAThreadAttribute()]
public static void Main()
{
Application.Run(new Form1());
} public Form1()
{
// Initialize the form.
// This code can be replaced with designer generated code.
AutoSize = true;
Text = "DataGridView IComparer sort demo"; FlowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
FlowLayoutPanel1.Location = new System.Drawing.Point( 304, 0 );
FlowLayoutPanel1.AutoSize = true; FlowLayoutPanel1.Controls.Add( RadioButton1 );
FlowLayoutPanel1.Controls.Add( RadioButton2 );
FlowLayoutPanel1.Controls.Add( Button1 ); Button1.Text = "Sort";
RadioButton1.Text = "Ascending";
RadioButton2.Text = "Descending";
RadioButton1.Checked = true; Controls.Add( FlowLayoutPanel1 );
Controls.Add( DataGridView1 );
} protected override void OnLoad( EventArgs e )
{
PopulateDataGridView();
Button1.Click += new EventHandler(Button1_Click); base.OnLoad( e );
} // Replace this with your own code to populate the DataGridView.
private void PopulateDataGridView()
{ DataGridView1.Size = new Size(300, 300); // Add columns to the DataGridView.
DataGridView1.ColumnCount = 2; // Set the properties of the DataGridView columns.
DataGridView1.Columns[0].Name = "First";
DataGridView1.Columns[1].Name = "Last";
DataGridView1.Columns["First"].HeaderText = "First Name";
DataGridView1.Columns["Last"].HeaderText = "Last Name";
DataGridView1.Columns["First"].SortMode =
DataGridViewColumnSortMode.Programmatic;
DataGridView1.Columns["Last"].SortMode =
DataGridViewColumnSortMode.Programmatic; // Add rows of data to the DataGridView.
DataGridView1.Rows.Add(new string[] { "Peter", "Parker" });
DataGridView1.Rows.Add(new string[] { "James", "Jameson" });
DataGridView1.Rows.Add(new string[] { "May", "Parker" });
DataGridView1.Rows.Add(new string[] { "Mary", "Watson" });
DataGridView1.Rows.Add(new string[] { "Eddie", "Brock" });
} private void Button1_Click( object sender, EventArgs e )
{
if ( RadioButton1.Checked == true )
{
DataGridView1.Sort( new RowComparer( SortOrder.Ascending ) );
}
else if ( RadioButton2.Checked == true )
{
DataGridView1.Sort( new RowComparer( SortOrder.Descending ) );
}
} private class RowComparer : System.Collections.IComparer
{
private static int sortOrderModifier = 1; public RowComparer(SortOrder sortOrder)
{
if (sortOrder == SortOrder.Descending)
{
sortOrderModifier = -1;
}
else if (sortOrder == SortOrder.Ascending)
{
sortOrderModifier = 1;
}
} public int Compare(object x, object y)
{
DataGridViewRow DataGridViewRow1 = (DataGridViewRow)x;
DataGridViewRow DataGridViewRow2 = (DataGridViewRow)y; // Try to sort based on the Last Name column.
int CompareResult = System.String.Compare(
DataGridViewRow1.Cells[1].Value.ToString(),
DataGridViewRow2.Cells[1].Value.ToString()); // If the Last Names are equal, sort based on the First Name.
if ( CompareResult == 0 )
{
CompareResult = System.String.Compare(
DataGridViewRow1.Cells[0].Value.ToString(),
DataGridViewRow2.Cells[0].Value.ToString());
}
return CompareResult * sortOrderModifier;
}
}
}