111,126
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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
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();
}
}
}
}