急啊!!!!!!!如何实现ListBox控件中项的更新??

liangzhl 2009-04-16 06:06:46
我在数据库中建了一个表,只有两列id,name 把他的name属性填充到ListBox控件,
ListBox.Items.Add(sdr["name"].ToString().Trim();
那当我选中了里面的一项时,她就显示在一个TextBox中,我要在TextBox中修改她,
然后按修改按钮,她就修改好了并又保存在数据库中 了??????
用C#怎么实现?????????????????
...全文
125 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
auqcffso 2009-04-21
  • 打赏
  • 举报
回复
好像可以这样子

你写一个包含ID,NAME的实体类
把所有数据查询出来后以实体类的形式放在集合中
然后把每个实体类的NAME加到LIXTBOX中,
当在LIXTBOX选中一项进行修改成功点确定的时候
这个时候就取出LISTBOX当前SELECTINDEX的索引值,拿到这个索引再用这个索引到前面保存实体类的集合取出位置相同的数据,不就可以实现了吗?

liangzhl 2009-04-21
  • 打赏
  • 举报
回复
楼上的,那 那个ListItem 怎么写啊???
阿非 2009-04-17
  • 打赏
  • 举报
回复
我真晕~ 我贴的不是winform 的么?
yagebu1983 2009-04-16
  • 打赏
  • 举报
回复
你应该把ID也绑定ListBox里,通过它来更新。。。
liangzhl 2009-04-16
  • 打赏
  • 举报
回复
可我要的是winform的啊
阿非 2009-04-16
  • 打赏
  • 举报
回复
他说的是asp.net
liangzhl 2009-04-16
  • 打赏
  • 举报
回复
listbox.Items.Add()
里只能加载一项啊?

那个AutoPostBack属性找不到?????????????

引用 3 楼 flightywxb 的回复:
protected void Page_Load(object sender, EventArgs e)
{
//循环{
ListBox1.Items.Add(new ListItem(sdr["name"].ToString().Trim(),sdr["id"].ToString().Trim()))
// }
}

//一定要设置ListBox1的AutoPostBack属性为true
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = ListBox1.SelectedItem.Text;

阿非 2009-04-16
  • 打赏
  • 举报
回复


namespace WindowsApplication1
{
partial class ListBox
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.lb = new System.Windows.Forms.ListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(167, 25);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// lb
//
this.lb.FormattingEnabled = true;
this.lb.ItemHeight = 12;
this.lb.Location = new System.Drawing.Point(33, 96);
this.lb.Name = "lb";
this.lb.Size = new System.Drawing.Size(120, 88);
this.lb.TabIndex = 1;
this.lb.SelectedIndexChanged += new System.EventHandler(this.lb_SelectedIndexChanged);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(33, 25);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 2;
//
// ListBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 270);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.lb);
this.Controls.Add(this.button1);
this.Name = "ListBox";
this.Text = "ListBox";
this.Load += new System.EventHandler(this.ListBox_Load);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox lb;
private System.Windows.Forms.TextBox textBox1;
}
}
阿非 2009-04-16
  • 打赏
  • 举报
回复


create table listBoxDB
(
[id] int primary key,
[name] varchar(50)
)
insert into listBoxDB select 1,'A'
union all select 2,'B'
union all select 3,'C'
union all select 4,'D'
union all select 5,'E'

select * from [listBoxDB]

--drop table listBoxDB


ListBox.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
public partial class ListBox : Form
{
DataSet ds = new DataSet();

public ListBox()
{
InitializeComponent();
}

private void ListBox_Load(object sender, EventArgs e)
{
DataBind();
}

private void DataBind()
{
using (SqlConnection con = new SqlConnection("data source=.;uid=sa;pwd=sa;database=test"))
{
try
{
string sql = "select * from [listBoxDB]";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
if (ds.Tables.Contains("listBoxDB"))
ds.Tables.Remove("listBoxDB");
da.Fill(ds, "listBoxDB");
lb.Items.Clear();
for (int i = 0; i < ds.Tables["listBoxDB"].Rows.Count; i++)
{
lb.Items.Add(ds.Tables["listBoxDB"].Rows[i]["Name"].ToString());
}
}
catch (Exception ee)
{
//...
}
finally
{
//con.Close();
}
}
}

private void button1_Click(object sender, EventArgs e)
{
if (lb.SelectedItem != null)
{
if (ds.Tables.Count > 0 && ds.Tables.Contains("listBoxDB"))
{
ds.Tables["listBoxDB"].Rows[lb.SelectedIndex]["Name"] = textBox1.Text;
using (SqlConnection con = new SqlConnection("data source=.;uid=sa;pwd=sa;database=test"))
{
try
{
string sql = "select * from [listBoxDB]";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
SqlCommandBuilder sqlBuild = new SqlCommandBuilder(da);

da.Update(ds.Tables["listBoxDB"]);
DataBind();
}
catch (Exception ee)
{
//...
}
finally
{
//con.Close();
}
}

}
}
}

private void lb_SelectedIndexChanged(object sender, EventArgs e)
{
if (lb.SelectedItem != null)
{
textBox1.Text = lb.SelectedItem.ToString();
}
}
}
}
teerhu 2009-04-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 flightywxb 的回复:]
protected void Page_Load(object sender, EventArgs e)
{
//循环{
ListBox1.Items.Add(new ListItem(sdr["name"].ToString().Trim(),sdr["id"].ToString().Trim()))
// }
}

//一定要设置ListBox1的AutoPostBack属性为true
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = ListBox1.SelectedItem.Text;

[/Quote]

UP
kyle123456 2009-04-16
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
//循环{
ListBox1.Items.Add(new ListItem(sdr["name"].ToString().Trim(),sdr["id"].ToString().Trim()))
// }
}

//一定要设置ListBox1的AutoPostBack属性为true
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = ListBox1.SelectedItem.Text;
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
ListBox1.SelectedItem.Text=TextBox1.Text ;
// 更新数据库操作……
//( 比如"update users set name='" +ListBox1.SelectedItem.Text+ "' where id='" +ListBox1.SelectedItem.Value+ "' " )
}
liangzhl 2009-04-16
  • 打赏
  • 举报
回复
winform中
阿非 2009-04-16
  • 打赏
  • 举报
回复
asp.net ? winform ?

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧