有一Combox 和一个listView
当选中ListView 中第三列的某一行时,该行的第三列变为Combox 选择Combox值后
该行第三列的值就改为Combox选中的值
首先listview 的属性选项中 在FULLROWSelect 选择true;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CreateListView();
listView1.Controls.Add(comboBox1);
}
public void CreateListView()
{
listView1.Sorting = SortOrder.Ascending;
for (int i = 1; i < 10; i++)
{
this.listView1.Columns.Add("第" + i + "列");
}
for (int j = 1; j < 50; j++)
{
ListViewItem item = new ListViewItem("第" + j + "行");
item.Checked = true;
for (int k = 1; k < 5; k++)
{
item.SubItems.Add("第" + j + "行" + k + "列");
}
listView1.Items.Add(item);
}
}
private Point point;
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
point.X = e.X;
point.Y= e.Y;
this.comboBox1.Visible = false;
ListViewItem item = this.listView1.GetItemAt(e.X, e.Y);
if (item != null)
{
ListViewItem.ListViewSubItem subItem = item.GetSubItemAt(e.X, e.Y);
int index = item.SubItems.IndexOf(subItem);
if (index == 3)
{
this.comboBox1.Visible = true;
comboBox1.Bounds = item.SubItems[3].Bounds;
}
}
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedText!=null)
{
ListViewItem item = this.listView1.GetItemAt(point.X, point.Y);
if (item != null)
{
ListViewItem.ListViewSubItem subItem = item.GetSubItemAt(point.X, point.Y);
int index = item.SubItems.IndexOf(subItem);
if (index == 3)
{
item.SubItems[index].Text = comboBox1.Text;
}
}
}
}
}
}