请问C#如何在listview中点击获取表头的值
3个回答
2011-09-23
展开全部
共有三种方法可以实现
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[] );
dataGridView1.Rows.Add(new string[] );
dataGridView1.Rows.Add(new string[] );
dataGridView1.Rows.Add(new string[] );
dataGridView1.Rows.Add(new string[] );
}
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;
}
}
}
3、使用 IComparer 接口自定义排序
使用 Sort 方法的 Sort(IComparer) 重载自定义排序,该重载采用 IComparer 接口的实现来执行多列排序。
#region Using directives
using System;
using System.Drawing;
using System.Windows.Forms;
#endregion
class 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[] );
DataGridView1.Rows.Add(new string[] );
DataGridView1.Rows.Add(new string[] );
DataGridView1.Rows.Add(new string[] );
DataGridView1.Rows.Add(new string[] );
}
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;
}
}
}
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[] );
dataGridView1.Rows.Add(new string[] );
dataGridView1.Rows.Add(new string[] );
dataGridView1.Rows.Add(new string[] );
dataGridView1.Rows.Add(new string[] );
}
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;
}
}
}
3、使用 IComparer 接口自定义排序
使用 Sort 方法的 Sort(IComparer) 重载自定义排序,该重载采用 IComparer 接口的实现来执行多列排序。
#region Using directives
using System;
using System.Drawing;
using System.Windows.Forms;
#endregion
class 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[] );
DataGridView1.Rows.Add(new string[] );
DataGridView1.Rows.Add(new string[] );
DataGridView1.Rows.Add(new string[] );
DataGridView1.Rows.Add(new string[] );
}
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;
}
}
}
展开全部
如果是一整行的数据,假设现在一行有4个数据吧。
如果要遍历的话用foreach来实现
int i=0;
StreamWriter SW=new StreamWriter(@"D:\test.txt",true);
foreach(ListViewItem LVI in this.ListView1.Items )
{
SW.Write(LVI.SubItem[0].text+"|");
SW.Write(LVI.SubItem[1].text+"|");
SW.Write(LVI.SubItem[2].text+"|");
SW.WriteLine(LVI.SubItem[3].text);
}
SW.Close();
上面是简单的实现将LivsView中的数据写入一个test.txt的文本文档中,每一行ListView的数据用 | 隔开,每写完一行后,转入下一行进行写数据,知道遍历完整个ListView的数据,此外当一行的数据很多的时候,也可以用SubItems.Counts来得到它的个数,并用循环分别读取,由于为了演示的方便,就直接列出了4个
如果要遍历的话用foreach来实现
int i=0;
StreamWriter SW=new StreamWriter(@"D:\test.txt",true);
foreach(ListViewItem LVI in this.ListView1.Items )
{
SW.Write(LVI.SubItem[0].text+"|");
SW.Write(LVI.SubItem[1].text+"|");
SW.Write(LVI.SubItem[2].text+"|");
SW.WriteLine(LVI.SubItem[3].text);
}
SW.Close();
上面是简单的实现将LivsView中的数据写入一个test.txt的文本文档中,每一行ListView的数据用 | 隔开,每写完一行后,转入下一行进行写数据,知道遍历完整个ListView的数据,此外当一行的数据很多的时候,也可以用SubItems.Counts来得到它的个数,并用循环分别读取,由于为了演示的方便,就直接列出了4个
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
2.用法:
# 排除当前虚拟主机需要正常访问的域名(web.eboat.cn )
# RewriteCond Host: (?:web|www)\.eboat\.cn
# 多数情况下是一个,即当前提供二级域名服务的系统(如建站系统)
RewriteCond Host: web\.eboat\.cn
RewriteRule (.*) $1 [L]
# 解决不规范目录(末尾无/)的问题
# 但前提是目录名不含‘.’而文件名必须包含,否则无法区分两者
# 如果保证URL中的目录名称规范,则无需此规则
RewriteRule ^/([^.]+[^/]$) /$1/ [L,R]
# 提取任意的二级域名名称(即第一个词汇)
# 用[a-zA-Z0-9_-]限制名称字符,重写Url到Home/子目录下
# 如 test.eboat.cn => web.eboat.cn/Home/test
RewriteCond Host: ([a-zA-Z0-9_-]+)\.eboat\.cn
RewriteRule ^/(.*) /Home/$1/$2 [I,L]
[ISAPI_Rewrite]
RepeatLimit 1
# 排除当前虚拟主机需要正常访问的域名(web.eboat.cn )
# RewriteCond Host: (?:web|www)\.eboat\.cn
# 多数情况下是一个,即当前提供二级域名服务的系统(如建站系统)
RewriteCond Host: web\.eboat\.cn
RewriteRule (.*) $1 [L]
# 解决不规范目录(末尾无/)的问题
# 但前提是目录名不含‘.’而文件名必须包含,否则无法区分两者
# 如果保证URL中的目录名称规范,则无需此规则
RewriteRule ^/([^.]+[^/]$) /$1/ [L,R]
# 提取任意的二级域名名称(即第一个词汇)
# 用[a-zA-Z0-9_-]限制名称字符,重写Url到Home/子目录下
# 如 test.eboat.cn => web.eboat.cn/Home/test
RewriteCond Host: ([a-zA-Z0-9_-]+)\.eboat\.cn
RewriteRule ^/(.*) /Home/$1/$2 [I,L]
[ISAPI_Rewrite]
RepeatLimit 1
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询