listview中的数据怎样修改? listview中的数据点击之后 再点击修改按钮 还有个添加数据的窗体 怎样在在这个添加数据的窗体显示这一行数据信息呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 listView1.SelectedItems[0].Text= ""; listView1.SelectedItems[0].SubItems[1].Text= ""; 我想你应该有用到泛型吧,当你选中ListView控件中某一行时,得到它的.Text值,根据这个值在泛型集合中去找,找到那组数据(如何保存看你自己,建议保存在Tag属性中)作为参数传给要打开窗体的构造函数(前提是要先重载添加数据窗体的构造函数),然后在添加数据窗体中取出传入的值即可。 试着根据提示自己去写代码,那样更能提高自己。 我也只是初学者,有不对的地方请多提意见,你们提出的意见是我最好的参考书。 如果你是用强类型,比如linq,可以参考如下代码绑定foreach(student item in db.students){ ListViewItem lvi=new ListViewItem(item.姓名)//一些字段 lvi.tag=item lvi.SubItems.Add(item.性别) this.listView1.Items.Add(lvi)}修改新增Form form=new 新增Form(this.ListView1.SelectItems[0].tag as student)if(form.ShowDialog()!=DialogResut.OK) return;然后在新增页面在加一些逻辑就可以额 添加窗体添加数据之后还要通过数据表的更新显示在listview上的 这个怎么办啊? listview有自己添加数据的模板 private void btnupdate_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count != 0) { Formadd1 fm = new Formadd1(); fm.txtname.Text = listView1.SelectedItems[0].Text; fm.txtsex.Text = listView1.SelectedItems[0].SubItems[1].Text; fm.dttm.Text = listView1.SelectedItems[0].SubItems[2].Text; fm.txtguoji.Text = listView1.SelectedItems[0].SubItems[3].Text; this.Hide(); fm.btnadd.Visible = false; fm.Show(); } else { MessageBox.Show("请选择要修改的信息"); return; } private void txtupdate_Click(object sender, EventArgs e) { string aname = this.txtname.Text.ToString(); string asex = this.txtsex.Text.ToString(); string abirthday = this.dttm.Text.ToString(); string anational = this.txtguoji.Text.ToString(); SqlConnection sqllog = new SqlConnection("Data Source=.;Initial Catalog=myktv;Integrated Security=True"); sqllog.Open(); SqlCommand sqlcom2 = new SqlCommand("update t_author set aname='"+aname+"',asex ='" + asex + "',abirthday='" + abirthday + "',anational='" + anational + "'where aname='" + aname + "'",sqllog); int row1 = sqlcom2.ExecuteNonQuery(); if (row1 > 0) { MessageBox.Show("修改成功"); this.Hide(); fm.tabControl1.SelectedIndex = 0; fm.Show(); }我把liatview的值传到from的textbox里面修改了 不过不能运行出来 大家看看哪错了 谢谢了 会不会那个listview还要再读一遍数据?还是没有更新啊?急求帮忙啊 谢谢了 由于构造函数的不同,决定了在form显示过程中都做哪些操作,示例代码public enum FormType{ NewForm, UpdateExisting}public class YourForm: Form{ private FormType formType = FormType.NewForm; private string mParam1; public void YourForm() { //这种情况意味着是一个新的form, formType = FormType.NewForm; } public void YourForm(string p1, string p2) { //这种情况意味着你要更新已有的数据 formType = FormType.UpdateExisting; this.mParam1 = p1; } private void YourForm_Load(object sender, EventArgs e) { switch(formType) { case FormType.NewForm: //新窗体时该如何处理 break; case FormType.UpdateExisting: //更新时如何处理 // 将mParam1的值显示在某处 break; 怎么获得本周周一的时间啊?求高手 请问:有没有Microsoft COM DLL相关资料? 如何最小化一个外部程序 大家做界面有没有什么好方法呀~~ 急,初学C#,进来看看,帮个忙! 急!大家帮帮我吧!关于file field控件上传文件的问题 问一个关于窗体的问题 图形报表!!!!! winForm下如何实现像电子表一样闪动的时间显示 如何在C#程序中将数据导出为html文件和.txt文件 C# winform如何判断窗口是否已经打开 DataTable输出行号
listView1.SelectedItems[0].SubItems[1].Text= "";
试着根据提示自己去写代码,那样更能提高自己。
我也只是初学者,有不对的地方请多提意见,你们提出的意见是我最好的参考书。
绑定foreach(student item in db.students)
{
ListViewItem lvi=new ListViewItem(item.姓名)//一些字段
lvi.tag=item
lvi.SubItems.Add(item.性别)
this.listView1.Items.Add(lvi)
}修改
新增Form form=new 新增Form(this.ListView1.SelectItems[0].tag as student)
if(form.ShowDialog()!=DialogResut.OK)
return;然后在新增页面在加一些逻辑就可以额
private void btnupdate_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count != 0)
{
Formadd1 fm = new Formadd1();
fm.txtname.Text = listView1.SelectedItems[0].Text;
fm.txtsex.Text = listView1.SelectedItems[0].SubItems[1].Text;
fm.dttm.Text = listView1.SelectedItems[0].SubItems[2].Text;
fm.txtguoji.Text = listView1.SelectedItems[0].SubItems[3].Text;
this.Hide();
fm.btnadd.Visible = false;
fm.Show();
}
else
{
MessageBox.Show("请选择要修改的信息");
return;
}
private void txtupdate_Click(object sender, EventArgs e)
{
string aname = this.txtname.Text.ToString();
string asex = this.txtsex.Text.ToString();
string abirthday = this.dttm.Text.ToString();
string anational = this.txtguoji.Text.ToString();
SqlConnection sqllog = new SqlConnection("Data Source=.;Initial Catalog=myktv;Integrated Security=True");
sqllog.Open();
SqlCommand sqlcom2 = new SqlCommand("update t_author set aname='"+aname+"',asex ='" + asex + "',abirthday='" + abirthday + "',anational='" + anational + "'where aname='" + aname + "'",sqllog);
int row1 = sqlcom2.ExecuteNonQuery();
if (row1 > 0)
{
MessageBox.Show("修改成功");
this.Hide();
fm.tabControl1.SelectedIndex = 0;
fm.Show();
}
我把liatview的值传到from的textbox里面修改了 不过不能运行出来 大家看看哪错了 谢谢了
public enum FormType
{
NewForm, UpdateExisting
}
public class YourForm: Form
{ private FormType formType = FormType.NewForm;
private string mParam1; public void YourForm()
{
//这种情况意味着是一个新的form,
formType = FormType.NewForm;
} public void YourForm(string p1, string p2)
{
//这种情况意味着你要更新已有的数据
formType = FormType.UpdateExisting;
this.mParam1 = p1;
} private void YourForm_Load(object sender, EventArgs e)
{
switch(formType)
{
case FormType.NewForm:
//新窗体时该如何处理
break;
case FormType.UpdateExisting:
//更新时如何处理
// 将mParam1的值显示在某处
break;