怎样让c# ListView实现以下功能
发布网友
发布时间:2022-05-12 07:33
我来回答
共5个回答
热心网友
时间:2024-02-19 17:55
1.先说书吧,两本我都看过,全是APRESS出版社的,感觉APRESS的书比O'REILLY结构更清晰,作者可全是微软MPV啊,
第二本绝对是winfoms的经典,在Amazon中很受好评,MATTHEW MACDONALD也是我的偶像,呵呵,这本书214页详细介绍ListView。还有推荐你看此书前学一学SQL,因为在Data Binding那几章频繁用到SQL语句。
第一本其实也不错,但不太适合新手,还有这本书有些地方不太合理,比如介绍Appdomain和MSIL,MSDN也不过几页,而他却大篇幅介绍,也些没必要。还有在在讲到多线程,没有提到NET 2中很有用的一个类:BackgroundWorker,总之。不适合新手,属于reference。
2.以下是控件代码,复制到你工程的namespace里,可直接在VS左侧Toolbox中拖放至窗体,取名Xshow。
class Xshow : ListView
{
public event EventHandler<DragEventArgs> XDragDrop;
public Xshow()
: base()
{
base.AllowDrop = true;
base.View = View.Details;
base.FullRowSelect = true;
base.ColumnClick += new ColumnClickEventHandler(Xshow_ColumnClick);
base.DragEnter += new DragEventHandler(Xshow_DragEnter);
base.DragDrop += new DragEventHandler(Xshow_DragDrop);
Columns.Add("Path",500);
}
class SortType : IComparer
{
int _subcolIdx;
bool _descending;
public int SubcolIdx
{
get { return _subcolIdx; }
set { _subcolIdx = value; }
}
public bool Descending
{
get { return _descending; }
set { _descending = value; }
}
public SortType(int subcolIdx)
{
_subcolIdx = subcolIdx;
_descending = false;
}
public int Compare(object a, object b)
{
ListViewItem x, y;
if (_descending) { x = (ListViewItem)b; y = (ListViewItem)a; }
else { x = (ListViewItem)a; y = (ListViewItem)b; }
//SubItems[0] == item in first row, SubItems[1]=first subitem
return String.Compare(x.SubItems[_subcolIdx].Text, y.SubItems[_subcolIdx].Text);
}
}
void Xshow_DragDrop(object sender, DragEventArgs e)
{
string[] ss = (string[])e.Data.GetData(DataFormats.FileDrop);
BeginUpdate();
foreach (string s in ss)
{
Items.Add(s);
}
EndUpdate();
//add your own Event Handler
XDragDrop(this, e);
}
void Xshow_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
else e.Effect = DragDropEffects.None;
}
void Xshow_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (Items.Count <= 1) return;
SortType st = ListViewItemSorter as SortType;
if (st == null) ListViewItemSorter = new SortType(e.Column);
else
{
st.Descending = !st.Descending; st.SubcolIdx = e.Column;
}
Sort();
}
}
热心网友
时间:2024-02-19 17:56
1) 首先listview.View = View.Details
listview排序事件是ColumnClick事件,添加该事件。
可以给这个Form申明一个bool型变量flg=true,ColumnClick每次处理后都将bool取反。
// 假设你存储数据的容器是个DataTable,也是最常见的
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e){
listView1.Items.Clear();
for(int i =0 ;i < dataTable.Rows.Count)
{
int m = i;
if(flg)
{
int m = dataTable.Rows.Count - i - 1;
}
String field1 = dataTable.Rows[m]["field1"].ToString();
String field2 = dataTable.Rows[m]["field2"].ToString();
String field3 = dataTable.Rows[m]["field3"].ToString();
listView1.Items.Add(new ListViewItem(new String[] { field1 , field2 , field3 }));
}
flg = !flg;
}
热心网友
时间:2024-02-19 17:56
第一个问题:
1。把view属性设为detail;
2.把headerStye属性设为clickable;
3.在columnclik事件中做排序处理
(做一个DataTable,再里面处理。倒着排序时,在DataTabl中取值的顺序可以最大INDE开始取。)
------------------------------------------
第四个问题:没有过看过,但是
现在的技术书籍大都商业性太强,实用性很差,
热心网友
时间:2024-02-19 17:57
1.鼠标点击column,实现排序,再点,就倒着排序,这个大家都知道吧,很常见把!
(1).把view属性设为detail;
(2).把headerStye属性设为clickable;
(3).在columnclik事件中做排序处理:
private bool _listViewSort = false;
// 列单击
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
_listViewSort = !_listViewSort;
Sort(listView1, e.Column, _listViewSort);
}
// 排序
private void Sort(ListView listView, int col, bool sortType)
{
IEnumerator iE = listView.Items.GetEnumerator();
int count = 0;
iE.Reset();
while (iE.MoveNext())
{
count++;
}
ListViewItem[] lVIs = new ListViewItem[count];
iE.Reset();
for (int i = 0; iE.MoveNext(); i++)
{
lVIs[i] = (ListViewItem)iE.Current;
}
int sortValue = 0;
if (sortType == true)
{
sortValue = 1;
}
else
{
sortValue = -1;
}
if (col == 0)
{
for (int i = lVIs.Length; i > 0; i--)
{
for (int j = 0; j < i - 1; j++)
{
if (lVIs[j].Text.CompareTo(lVIs[j + 1].Text) == sortValue)
{
ListViewItem lVI = lVIs[j];
lVIs[j] = lVIs[j + 1];
lVIs[j + 1] = lVI;
}
}
}
}
else
{
for (int i = lVIs.Length; i > 0; i--)
{
for (int j = 0; j < i - 1; j++)
{
if (lVIs[j].SubItems[col].Text.CompareTo(lVIs[j + 1].SubItems[col].Text) == sortValue)
{
ListViewItem lVI = lVIs[j];
lVIs[j] = lVIs[j + 1];
lVIs[j + 1] = lVI;
}
}
}
}
listView.Items.Clear();
foreach (ListViewItem lVI in lVIs)
{
listView.Items.Add(lVI);
}
}
2.从资源管理器里支持拖放文件和文件夹,并表明他的类型。
private void MainForm_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void MainForm_DragDrop(object sender, DragEventArgs e)
{
Array arrayFileName = (Array)e.Data.GetData(DataFormats.FileDrop);
List<string> fileList = new List<string>();
for (int i = 0; i < arrayFileName.Length; i++)
{
string strFileName = arrayFileName.GetValue(i).ToString();
if (Directory.Exists(strFileName))
{
GetFileNames(strFileName, fileList);
}
else if (File.Exists(strFileName))
{
fileList.Add(strFileName);
}
}
string[] files = new string[fileList.Count];
for (int i = 0; i < files.Length; i++)
{
files[i] = fileList[i];
}
AddFiles(files);
}
3.最好写成一个控件。
自己写
4.谁看过Pro C# 2005 and the .NET 2.0 Platform和Net 2.0 Windows Forms And Custom Controls In C#这两本书,怎么样?
没看过,不好评论
1,2没有明白的话,可以发消息问我,
热心网友
时间:2024-02-19 17:57
http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece763104c8c711923d030678197027fa3c215cc79031a1b33a6ec7c745719839b21321cf40f0bb5ed6573315725e4caccdc4dd9b9c3782c832c657318871715d40eafbc1c639e7e841fb4ef4fe8e4ab73d5ea89849a050e94145a26c1adcb1c5754&p=8639c64ad7af13f708e29779130e&user=