• 大小: 20KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-04-13
  • 语言: C#
  • 标签: C#  

资源简介

C#使用数据绑定控件录入数据,一个基本的窗体录入程序,不同的是录入的方式 ,是采用数据绑定的方式,数据可解析回数据库

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Data.SqlClient;
//Download by http://www.codefans.net
namespace DataAbduct
{
    public partial class Form1 : Form
    {
        DataTable dt=null;
        TextBox[] txtBox;
        SqlConnection con = new SqlConnection(“server=.;pwd=;uid=sa;database=db_09“);
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender EventArgs e)
        {
            showList();
        }
        private void showList()
        {
            listView1.View = View.Details;//视图
            listView1.GridLines = true;//网格线
            using (SqlDataAdapter da = new SqlDataAdapter(“select * from 帐单“ con))
            {
                //生成结果集
                dt = new DataTable();
                da.Fill(dt);
                ColumnHeader ch;
                for (int i = 0; i < dt.Columns.Count; i++)//列
                {
                    ch = new ColumnHeader();
                    ch.Text = dt.Columns[i].ColumnName.ToString();
                    ch.Name = dt.Columns[i].ColumnName.ToString();
                    ch.Width = 72;
                    this.listView1.Columns.Add(ch);
                }
                //建立结构
                Method(dt);
            }      
        }
        private void tbADD_Click(object sender EventArgs e)
        {
            if (dt != null)
            {
                DataRow row;
                txtBox = new TextBox[6];
                txtBox[0] = this.textBox1;
                txtBox[1] = this.textBox2;
                txtBox[2] = this.textBox3;
                txtBox[3] = this.textBox4;
                txtBox[4] = this.textBox5;
                txtBox[5] = this.textBox6;
                row = dt.NewRow();
                for (int i = 0; i < dt.Columns.Count; i++)
                {            
                   row[dt.Columns[i].ToString()] = this.txtBox[i].Text.ToString();
                }
                dt.Rows.Add(row);
                Method(dt);
            }
        }

        private void Method(DataTable dt)
        {
            listView1.Items.Clear();
            ListViewItem listItem = null;
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                listItem = new ListViewItem(dt.Rows[j][0].ToString());
                for (int k = 1; k < dt.Columns.Count; k++)
                {
                    listItem.SubItems.Add(dt.Rows[j][k].ToString());
                }
                listView1.Items.Add(listItem);
            }
        }

        private void tbSave_Click(object sender EventArgs e)
        {
            using (SqlDataAdapter da = new SqlDataAdapter())
            {
                SqlCommand command = new SqlCommand(“INSERT INTO 帐单 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3766  2020-04-02 21:42  DataAbduct\DataAbduct\DataAbduct.csproj

     文件       3967  2020-04-02 21:42  DataAbduct\DataAbduct\Form1.cs

     文件      11909  2020-04-02 21:42  DataAbduct\DataAbduct\Form1.Designer.cs

     文件      15407  2020-04-02 21:42  DataAbduct\DataAbduct\Form1.resx

     文件        528  2020-04-02 21:42  DataAbduct\DataAbduct\Program.cs

     文件       1352  2020-04-02 21:42  DataAbduct\DataAbduct\Properties\AssemblyInfo.cs

     文件       2870  2020-04-02 21:42  DataAbduct\DataAbduct\Properties\Resources.Designer.cs

     文件       5612  2020-04-02 21:42  DataAbduct\DataAbduct\Properties\Resources.resx

     文件       1095  2020-04-02 21:42  DataAbduct\DataAbduct\Properties\Settings.Designer.cs

     文件        249  2020-04-02 21:42  DataAbduct\DataAbduct\Properties\Settings.settings

     文件        920  2020-04-02 21:42  DataAbduct\DataAbduct.sln

     文件      17408  2020-04-02 21:42  DataAbduct\DataAbduct.suo

     目录          0  2020-04-02 21:42  DataAbduct\DataAbduct\Properties

     目录          0  2020-04-02 21:42  DataAbduct\DataAbduct

     目录          0  2020-04-02 21:42  DataAbduct

----------- ---------  ---------- -----  ----

                65083                    15


评论

共有 条评论