C# 通过listbox连接数据库相关操作,演示
一个简单的程序,通过listbox,操作access数据。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 物资管理系统
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“dataSet1.wzb”中。您可以根据需要移动或移除它。
this.wzbTableAdapter.Fill(this.dataSet1.wzb);
listBox1.SelectedIndex = 0;
DataRowView my_row = (DataRowView)(listBox1.SelectedItem);
//tabpage1填充
lbl_xx_bh.Text = my_row[0].ToString();
txt_xx_mc.Text = my_row[1].ToString();
txt_xx_sl.Text = my_row[2].ToString();
txt_xx_dw.Text = my_row[3].ToString();
//tabpage3填充
txt_xg_bh.Text = my_row[0].ToString();
txt_xg_mc.Text = my_row[1].ToString();
txt_xg_sl.Text = my_row[2].ToString();
txt_xg_dw.Text = my_row[3].ToString();
}
private void button3_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
DataRowView rw = (DataRowView)(listBox1.SelectedItem);
rw.Delete();
}
}
private void button5_Click(object sender, EventArgs e)
{
this.wzbTableAdapter.Update(this.dataSet1);
this.wzbTableAdapter.Fill(this.dataSet1.wzb);
}
private void button6_Click(object sender, EventArgs e)
{
this.dataSet1.RejectChanges();
this.wzbTableAdapter.Fill(this.dataSet1.wzb);
}
private void button2_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
//tabpage1填充
DataRowView my_row = (DataRowView)(listBox1.SelectedItem);
lbl_xx_bh.Text = my_row[0].ToString();
txt_xx_mc.Text = my_row[1].ToString();
txt_xx_sl.Text = my_row[2].ToString();
txt_xx_dw.Text = my_row[3].ToString();
//tabpage3填充
txt_xg_bh.Text = my_row[0].ToString();
txt_xg_mc.Text = my_row[1].ToString();
txt_xg_sl.Text = my_row[2].ToString();
txt_xg_dw.Text = my_row[3].ToString();
}
else
{
//tabpage1填充
lbl_xx_bh.Text ="";
txt_xx_mc.Text ="";
txt_xx_sl.Text ="";
txt_xx_dw.Text ="";
//tabpage3填充
txt_xg_bh.Text ="";
txt_xg_mc.Text ="";
txt_xg_sl.Text ="";
txt_xg_dw.Text ="";
}
}
private void button8_Click(object sender, EventArgs e)
{
int num = 0;
if (txt_add_bh.Text == "")
{
MessageBox.Show("请输入物资编号");
return;
}
if (txt_add_mc.Text == "")
{
MessageBox.Show("请输入物资名称");
return;
}
if (int.TryParse(txt_add_bh.Text, out num) && int.TryParse(txt_add_sl.Text, out num))
{
// MessageBox.Show("编号为数字");可以继续进行
if (Convert.ToInt32(txt_add_bh.Text) < 0 || Convert.ToInt32(txt_add_sl.Text) < 0)
{
MessageBox.Show("编号和数量必须大于等于0!");
return;
}
DataRow row = this.dataSet1.wzb.NewRow();
string txt_bh_temp="";
for (int i = 0; i < 8 - txt_add_bh.Text.Length; i++)
txt_bh_temp = txt_bh_temp + "0";
txt_add_bh.Text = txt_bh_temp+txt_add_bh.Text ;
row[0] = txt_add_bh.Text;
row[1] = txt_add_mc.Text;
row[2] = txt_add_sl.Text;
row[3] = txt_add_dw.Text;
try
{
this.dataSet1.wzb.Rows.Add(row);
}
catch
{
MessageBox.Show("添加产生了错误,可能编号已存在。");
}
}
else
{
MessageBox.Show("编号和数量必须为数字");
return;
}
}
private void button7_Click(object sender, EventArgs e)
{
txt_add_bh.Text = "";
txt_add_sl.Text = "";
txt_add_dw.Text = "";
txt_add_mc.Text = "";
}
private void button2_Click_1(object sender, EventArgs e)
{
txt_xg_bh.Text = "";
txt_xg_sl.Text = "";
txt_xg_dw.Text = "";
txt_xg_mc.Text = "";
}
private void button9_Click(object sender, EventArgs e)
{
int num = 0;
if (txt_xg_bh.Text == "")
{
MessageBox.Show("请输入物资编号");
return;
}
if (txt_xg_mc.Text == "")
{
MessageBox.Show("请输入物资名称");
return;
}
if (int.TryParse(txt_xg_bh.Text, out num) && int.TryParse(txt_xg_sl.Text, out num) )
{
// MessageBox.Show("编号为数字");可以继续进行
if (Convert.ToInt32(txt_xg_bh.Text) < 0 || Convert.ToInt32(txt_xg_sl.Text) < 0)
{
MessageBox.Show("编号和数量必须大于等于0!");
return;
}
if (listBox1.SelectedIndex != -1)
{
int index = listBox1.SelectedIndex;
DataRowView row = (DataRowView)listBox1.SelectedItem;
DataRow my_row = this.dataSet1.wzb.Rows.Find(row[0]);
if (my_row != null)
{
my_row.BeginEdit();
my_row[0] = txt_xg_bh.Text;
my_row[1] = txt_xg_mc.Text;
my_row[2] = txt_xg_sl.Text;
my_row[3] = txt_xg_dw.Text;
my_row.EndEdit();
}
listBox1.SelectedIndex = index;
}
}
else
{
MessageBox.Show("编号和数量必须为数字");
return;
}
}
private void btn_feipei_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 2;
}
}
}