listview 列随lietview大小自动平分适应listview大小
private void listview_SizeChanged(object sender, EventArgs e)
{
int _Count = listview.Columns.Count;
// 如果需要去掉水平滚顶条,可以将listview.Width减去一部分,例如:
// int _Width = listview.Width - 17;
int _Width = listview.Width;
foreach (ColumnHeader ch in listview.Columns)
{
ch.Width = _Width / _Count;
}
}
listview列表头改变背景颜色和文本颜色
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
int tColumnCount;
Rectangle tRect = new Rectangle();
Point tPoint = new Point();
Font tFont = new Font("微软雅黑", 9, FontStyle.Regular);
/*SolidBrush tBackBrush = new SolidBrush(Color.FromArgb(((int)(((byte)(56)))), ((int)(((byte)(56)))), ((int)(((byte)(56))))));*/
SolidBrush tBackBrush = new SolidBrush(Color.Transparent);
SolidBrush tFtontBrush;
tFtontBrush = new SolidBrush(SystemColors.GradientActiveCaption);
if (listView1.Columns.Count == 0)
return;
tColumnCount = listView1.Columns.Count;
tRect.Y = 0;
tRect.Height = e.Bounds.Height - 1;
tPoint.Y = 3;
for (int i = 0; i < tColumnCount; i++)
{
if (i == 0)
{
tRect.X = 0;
tRect.Width = listView1.Columns[i].Width;
}
else
{
tRect.X += tRect.Width;
tRect.X += 1;
tRect.Width = listView1.Columns[i].Width - 1;
}
e.Graphics.FillRectangle(tBackBrush, tRect);
tPoint.X = tRect.X + 3;
e.Graphics.DrawString(listView1.Columns[i].Text, tFont, tFtontBrush, tPoint);
}
}
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
listview动态插入数据
private void button1_Click_1(object sender, EventArgs e)
{
方法1(交错数组,简单说是数组的数组)
string[][] xxx = new string[10][];
xxx[0] = new string[] { "1", "2", "3" ,"4"};
xxx[1] = new string[] { "4", "5", "6" };
xxx[2] = new string[] { "7", "8", "9" };
xxx[3] = new string[] { "10", "11", "12" };
xxx[4] = new string[] { "13", "14", "15" };
xxx[5] = new string[] { "16", "17", "18" };
xxx[6] = new string[] { "19", "20", "21" };
xxx[7] = new string[] { "22", "23", "24" };
xxx[8] = new string[] { "25", "26", "27" };
xxx[9] = new string[] { "28", "29", "30" };
for (int i = 0; i < xxx.Length; i++)
{
ListViewItem item = new ListViewItem(xxx[i]);
listView1.Items.Add(item);
}
this.listView1.EndUpdate();
方法2(与方法4类似)
ListViewItem item = new ListViewItem(); //先实例化ListViewItem这个类
item.Text = "1"; //添加第1列内容,注意是"Text"
item.SubItems.Add("2"); //添加第2列内容
item.SubItems.Add("3"); //添加第3例内容
listView1.Items.Add(item); //集体添加进去
**方法3**
listView1.Items.Add(new ListViewItem(new string[] { "a", "b","c","d" }));
**方法4 (最好理解)**
ListViewItem LVI = new ListViewItem("11"); //11是String型,为第1列内容
LVI.SubItems.Add("22");
LVI.SubItems.Add("33");
listView1.Items.Add(LVI);
**方法5 (两个数据内容填充到同一行中的多列)**
for (int i = 0; i < 6; i++)
{
//这里i需要是字符型的,所以转换一下,i为第1列的内容
ListViewItem item = new ListViewItem(Convert.ToString(i));
string[] first = { "a", "b", "c" };
string[] second = { "1", "2", "3" };
item.SubItems.AddRange(first);
item.SubItems.AddRange(second);
listView3.Items.Add(item);
}
listview设置指定单元的文本颜色
ListViewItem item1 = listView3.Items[1];
ListViewSubItemCollection list = listView3.Items[1].SubItems;
ListViewSubItem subItem = list[1];
item1.UseItemStyleForSubItems = false;
subItem.ForeColor = Color.CornflowerBlue;
listview添加图片
1.首先拖控件imageList到窗体;
2.设置imageList属性“ColorDepth”为“Depth32Bit”;
3.设置imageList属性“imageSize”为图片的像素大小(例如:通过鼠标右键某文件夹下的某张图片点击属性,就可以找到像素大小,而像素大小就需要跟imageList控件中图片尺寸大小匹配,否则也许会失真。)
4.一定要最后在添加图片,否则设置的属性值都不起效果。
5.设置ListView控件中属性“SmallImageList”为刚添加的imageList控件。
6.如下代码:
ListViewItem item1 = listView3.Items[某一行的索引值];
listViewItem.ImageIndex = imageList控件中的某张图片索引;