弹出页面
主页面(FrmIn) FrmSelectEr frmSelect = new FrmSelectEr();
frmSelect.Owner = this;
frmSelect.ShowDialog();
弹出页面(frmSelect) 获取主页面控件值
FrmIn Frmin = (FrmIn)this.Owner;
Frmin.T_C_Name.Text ="XXX";
datagridview和listview 获取选中行,第一例的值
listview : list.SelectedItems[0].SubItems[0].Text;
datagridview:dataGridView1.SelectedRows[0].Cells[0].Value.ToString()
弹出确认框
if (MessageBox.Show("是否真的要修改该房间信息? ", "确定操作 ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{ xxxxx }
listview 赋值
public void databind(string SelectSQL)
{
list.Items.Clear();
DataSet ds = bll_customer.GetList(SelectSQL);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
ListViewItem lvt = new ListViewItem(ds.Tables[0].Rows[i]["C_ID"].ToString().Trim());
lvt.SubItems.Add(ds.Tables[0].Rows[i]["C_Name"].ToString().Trim());
lvt.SubItems.Add(ds.Tables[0].Rows[i]["C_Sex"].ToString().Trim());
lvt.SubItems.Add(ds.Tables[0].Rows[i]["C_Money"].ToString().Trim());
lvt.SubItems.Add(ds.Tables[0].Rows[i]["C_dengjiTime"].ToString().Trim());
list.Items.Add(lvt);
}
}
}
=================
datagivdview赋值
public void databind()
{
DataSet ds_room = bll_room.GetAllList();
ds_room.Tables[0].Columns.Add();
ds_room.Tables[0].Columns.Add();
for (int i = 0; i < ds_room.Tables[0].Rows.Count; i++)
{
DataColumn dc = new DataColumn();
RoomFee rf = bll_RoomType.GetModel(ds_room.Tables[0].Rows[i]["RF_ID"].ToString());
ds_room.Tables[0].Rows[i][5] = rf.RF_Name.Trim();
ds_room.Tables[0].Rows[i][6] = rf.RF_Money.ToString();
}
dataGridViewX1.DataSource = ds_room.Tables[0];
dataGridViewX1.Columns[0].HeaderText = "房间号";
dataGridViewX1.Columns[1].Visible = false;
dataGridViewX1.Columns[2].HeaderText = "在住人数";
dataGridViewX1.Columns[3].HeaderText = "状态";
dataGridViewX1.Columns[4].HeaderText = "房间备注";
dataGridViewX1.Columns[5].HeaderText = "房间类别";
dataGridViewX1.Columns[6].HeaderText = "房间价格";
}
====-
listview 双击单元格事件
private void list_DoubleClick(object sender, EventArgs e)
{
if (list.SelectedItems.Count == 0) return;
FrmIn Frmin = (FrmIn)this.Owner;
if (Frmin == null)
{
return;
}
Frmin.T_C_Name.Text = list.SelectedItems[0].SubItems[1].Text;
Frmin.T_C_ID.Text = list.SelectedItems[0].SubItems[0].Text;
this.Close();
}
=====================
datagridview 鼠标单击事件,给textbox赋值
private void dataGridViewX1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
buttonX1.Enabled = false;
T_R_No.Text = dataGridViewX1.Rows[e.RowIndex].Cells[0].Value.ToString();
C_RoomType.SelectedText = dataGridViewX1.Rows[e.RowIndex].Cells[3].Value.ToString();
C_RoomType.SelectedValue = dataGridViewX1.Rows[e.RowIndex].Cells[1].Value.ToString();
T_R_mark.Text = dataGridViewX1.Rows[e.RowIndex].Cells[4].Value.ToString();
// T_RF_Num.Text = roomfee.RF_num.ToString(); ;
}
======================
contextMenuStrip 控件 右键快捷方式
private void listViewEx1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && listViewEx1.SelectedItems.Count == 1)
{
contextMenuStrip1.Show(MousePosition);
}
}
=======================
窗体第一次出现位置
窗体的StarPosition属性值为centerscreen就能居中显示
===========================
1. 常用属性设置
-外观 FullRowSelect True //选中整行
GridLines True //显示网格线
View Details
2. 选中整行触发的事件
ItemSelectionChanged
==双击单元格事件
3. 添加元素
ListViewItem li = new ListViewItem();
li.SubItems[0].Text = ".....";
li.SubItems.Add(...);
......
listView1.Items.Add(li);
4. 点击ListView列表头进行排序
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (listView1.ListViewItemSorter == null)
listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
else
{
ListViewItemComparer comparer = listView1.ListViewItemSorter as ListViewItemComparer;
if (comparer.SortColumn == e.Column)
{
if (comparer.Order == SortOrder.Ascending)
comparer.Order = SortOrder.Descending;
else
comparer.Order = SortOrder.Ascending;
listView1.Sort();
}
else
listView1.ListViewItemSorter = new ListViewItemComparer(e.Column);
}
}
private class ListViewItemComparer : System.Collections.IComparer
{
public int SortColumn;
public SortOrder Order;
public ListViewItemComparer()
{
this.SortColumn = 0;
this.Order = SortOrder.Ascending;
}
public ListViewItemComparer(int column) : this()
{
this.SortColumn = column;
}
public ListViewItemComparer(int column, SortOrder sortOrder) : this(column)
{
this.Order = sortOrder;
}
#region IComparer Members
public int Compare(object x, object y)
{
int result = string.Compare(((ListViewItem)x).SubItems[this.SortColumn].Text,
((ListViewItem)y).SubItems[this.SortColumn].Text);
if (this.Order == SortOrder.Ascending)
return result;
else if (this.Order == SortOrder.Descending)
return (-result);
else return 0;
}
#endregion
}
在开发中使用ListView控件的时候,垂直滚动条不能时刻保持在最底部,这样导致,新插入的数据不能直观的
看见。使用下面方法可以解决这个功能
this.LV_Trace.Items[this.LV_Trace.Items.Count-1].EnsureVisible();
这样,就可以使垂直滚动条时刻保持在底部,这样,新插入的数据就可以很好的显示了。
//更改属性
this.listView1.GridLines = true; //显示表格线
this.listView1.View = View.Details;//显示表格细节
this.listView1.LabelEdit = true; //是否可编辑,ListView只可编辑第一列。
this.listView1.Scrollable = true;//有滚动条
this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;//对表头进行设置
this.listView1.FullRowSelect = true;//是否可以选择行
//this.listView1.HotTracking = true;// 当选择此属性时则HoverSelection自动为true和Activation属性为oneClick
//this.listView1.HoverSelection = true;
//this.listView1.Activation = ItemActivation.Standard; //
//添加表头
this.listView1.Columns.Add("", 0);
this.listView1.Columns.Add("列1",80);
this.listView1.Columns.Add("列2", 160);
//添加各项
ListViewItem[] p = new ListViewItem[2];
p[0] = new ListViewItem(new string[] { "","aaaa","bbbb"});
p[1] = new ListViewItem(new string[] { "","cccc", "ggggg" });
//p[0].SubItems[0].BackColor = Color.Red; //用于设置某行的背景颜色
this.listView1.Items.AddRange(p);
//也可以用this.listView1.Items.Add();不过需要在使用的前后添加Begin... 和End...防止界面自动刷新
// 添加分组
this.listView1.Groups.Add(new ListViewGroup("tou"));
this.listView1.Groups.Add(new ListViewGroup("wei"));
this.listView1.Items[0].Group = this.listView1.Groups[0];
this.listView1.Items[1].Group = this.listView1.Groups[1];
listview,怎么也没找到从那个地方调整item的行高,找来找去也没发现,真想大骂微软一下,仔细思考一下,因该有2种方式,一种重写listview,另外一种listview肯定有相关属性来调整行高,功夫不负有心人。
网上有个写高人,通过如下方式来实现
1,添加一imagelist控件
2,指定listview的smallimages属性为imagelist1
3,只需要调整imagelist中图像高度.listview控件中的高度随之调整。
非常简单的小问题
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@22
listView1.View = View.Details;//listView显示方式
listView1.Columns.Add("进程名");//增加项目标题
listView1.Columns.Add("路径");//增加项目标题
listView1.Columns.Add("进程名");//增加项目标题
//添加一行项目
listView1.Items.Add("name", "QQ.exe", 0);
listView1.Items["name"].SubItems.Add("c:\\QQ.exe");
listView1.Items["name"].SubItems.Add("pin");
//@@@@@@@@@@@@第二种方式
listView1.BeginUpdate();
this.listView1.GridLines = true; //显示表格线
this.listView1.FullRowSelect = true;//是否可以选择行
this.listView1.Scrollable = true;//有滚动条
listView1.View = View.Details;//listView显示方式
listView1.Columns.Add("文件名",100);//创建标题
listView1.Columns.Add("大小",50);
listView1.Columns.Add("創建日期",150);
ListViewItem lvItem = new ListViewItem();// 定义添加第一个项目
lvItem.Text = "文件夾1";
listView1.Items.Add(lvItem);
ListViewItem.ListViewSubItem lvSubItem = new ListViewItem.ListViewSubItem();//定义第一个子项目
lvSubItem.Text = "10";
lvItem.SubItems.Add(lvSubItem);
lvSubItem = new ListViewItem.ListViewSubItem();//添加第二个子项目
lvSubItem.Text = "20080114";
lvItem.SubItems.Add(lvSubItem);
lvItem = new ListViewItem();//添加第二个项目
lvItem.Text = "文件夾2";
lvSubItem = new ListViewItem.ListViewSubItem();
lvSubItem.Text = "20";
lvItem.SubItems.Add(lvSubItem);
lvSubItem = new ListViewItem.ListViewSubItem();
lvSubItem.Text = "20080115";
lvItem.SubItems.Add(lvSubItem);
listView1.Items.Add(lvItem);
listView1.EndUpdate();
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
/更改属性
this.listView1.GridLines = true; //显示表格线
this.listView1.View = View.Details;//显示表格细节
this.listView1.LabelEdit = true; //是否可编辑,ListView只可编辑第一列。
this.listView1.Scrollable = true;//有滚动条
this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;//对表头进行设置
this.listView1.FullRowSelect = true;//是否可以选择行
//this.listView1.HotTracking = true;// 当选择此属性时则HoverSelection自动为true和Activation属性为oneClick
//this.listView1.HoverSelection = true;
//this.listView1.Activation = ItemActivation.Standard; //
//添加表头
this.listView1.Columns.Add("", 0);
this.listView1.Columns.Add("列1",80);
this.listView1.Columns.Add("列2", 160);
//添加各项
ListViewItem[] p = new ListViewItem[2];
p[0] = new ListViewItem(new string[] { "","aaaa","bbbb"});
p[1] = new ListViewItem(new string[] { "","cccc", "ggggg" });
//p[0].SubItems[0].BackColor = Color.Red; //用于设置某行的背景颜色
this.listView1.Items.AddRange(p);
//也可以用this.listView1.Items.Add();不过需要在使用的前后添加Begin... 和End...防止界面自动刷新
// 添加分组
this.listView1.Groups.Add(new ListViewGroup("tou"));
this.listView1.Groups.Add(new ListViewGroup("wei"));
this.listView1.Items[0].Group = this.listView1.Groups[0];
this.listView1.Items[1].Group = this.listView1.Groups[1];
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//实现选中某行右键弹出菜单,添加contextmenustrip控件,
//listView1.ContextMenuStrip = this.contextMenuStrip1;
//添加鼠标右击弹出菜单,使listview与contextMenu连接
if (listView1.SelectedItems.Count > 0)
{
ListViewItem item = listView1.SelectedItems[0];
string filename = item.Text;
//string pName = item.SubItems[1].Text;
//MessageBox.Show (String, String, MessageBoxButtons, MessageBoxIcon)
DialogResult dr = MessageBox.Show(string.Format("确定要删除 {0}文件吗?", filename),
"警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (dr == DialogResult.OK)
{
try
{
File.Delete(filename);
}
catch
{
MessageBox.Show("文件删除失败");
}
}
}