苏飞论坛

标题: 大神帮帮忙C###datagridview,菜鸟很无助 [打印本页]

作者: 沙子0329    时间: 2014-5-17 13:05
标题: 大神帮帮忙C###datagridview,菜鸟很无助
本帖最后由 沙子0329 于 2014-5-17 13:23 编辑

DataGrideView给表添加一行数据。本来都调试好了,可是`````换了了个数据源。大BUG出现了......

作者: 沙子0329    时间: 2014-5-17 13:08
我是初学者  求大神帮帮忙
作者: 沙子0329    时间: 2014-5-17 13:24
帮帮忙
作者: 沙子0329    时间: 2014-5-17 13:35
有人吗
作者: 站长苏飞    时间: 2014-5-17 13:53
贴下代码看看
作者: 沙子0329    时间: 2014-5-17 14:07
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace studentmanagement
{
    public partial class informanage : Form
    {
        SqlDataAdapter adapter;
        DataTable selectedTable;

        public informanage()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Dispose();
            teacherform teacherform = new teacherform();
            teacherform.Show();
        }

        private void informanage_Load(object sender, EventArgs e)
        {
            // TODO:  这行代码将数据加载到表“sinfodatebaseDataSet.sinfor”中。您可以根据需要移动或删除它。
          this.sinforTableAdapter.Fill(this.sinfodatebaseDataSet.sinfor);
           // BindingSource sinforBindingSource = new BindingSource();
           // sinforBindingSource.DataSource = sinfodatebaseDataSet.sinfor;
            bindingNavigator1.BindingSource = bindingSource1;
            for (int i = 0; i < sinfodatebaseDataSet7.Tables.Count; i++)
            {
                listBox1.Items.Add(sinfodatebaseDataSet7.Tables[i].TableName);
            }
            listBox1.SelectedIndex = 0;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;

            ;
            //sinfodatebaseDataSet1TableAdapters.sinforTableAdapter adapter = new sinfodatebaseDataSetTableAdapters.sinforTableAdapter();
            //adapter.Fill(this.sinfodatebaseDataSet.sinfor);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSource1.AddNew();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请单击选择要删除的行");
            }
            else
            {
                if (MessageBox.Show("确定要删除这行吗", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    for (int i = dataGridView1.SelectedRows.Count - 1; i >= 0; i--)
                    {
                        dataGridView1.Rows.Remove(dataGridView1.SelectedRows[i]);
                    }
                }

            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            selectedTable = sinfodatebaseDataSet7.Tables[index];
            string queryString = "select * from " + selectedTable.TableName;
            adapter = new SqlDataAdapter(queryString, Properties.Settings.Default.sinfodatebaseConnectionString);
            SqlCommandBuilder builer = new SqlCommandBuilder(adapter);
            adapter.InsertCommand = builer.GetInsertCommand();
            adapter.DeleteCommand = builer.GetDeleteCommand();
            adapter.UpdateCommand = builer.GetUpdateCommand();
            adapter.Fill(selectedTable);
            bindingSource1.DataSource = selectedTable;
            dataGridView1.DataSource = bindingSource1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                bindingSource1.EndEdit();
                adapter.Update(sinfodatebaseDataSet7.Tables[listBox1.SelectedIndex]);
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "保存失败");
            }
        }

作者: 沙子0329    时间: 2014-5-17 14:09
站长苏飞 发表于 2014-5-17 13:53
贴下代码看看

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace studentmanagement
{
    public partial class informanage : Form
    {
        SqlDataAdapter adapter;
        DataTable selectedTable;

        public informanage()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Dispose();
            teacherform teacherform = new teacherform();
            teacherform.Show();
        }

        private void informanage_Load(object sender, EventArgs e)
        {
            // TODO:  这行代码将数据加载到表“sinfodatebaseDataSet.sinfor”中。您可以根据需要移动或删除它。
          this.sinforTableAdapter.Fill(this.sinfodatebaseDataSet.sinfor);
           // BindingSource sinforBindingSource = new BindingSource();
           // sinforBindingSource.DataSource = sinfodatebaseDataSet.sinfor;
            bindingNavigator1.BindingSource = bindingSource1;
            for (int i = 0; i < sinfodatebaseDataSet7.Tables.Count; i++)
            {
                listBox1.Items.Add(sinfodatebaseDataSet7.Tables.TableName);
            }
            listBox1.SelectedIndex = 0;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;

            ;
            //sinfodatebaseDataSet1TableAdapters.sinforTableAdapter adapter = new sinfodatebaseDataSetTableAdapters.sinforTableAdapter();
            //adapter.Fill(this.sinfodatebaseDataSet.sinfor);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSource1.AddNew();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请单击选择要删除的行");
            }
            else
            {
                if (MessageBox.Show("确定要删除这行吗", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    for (int i = dataGridView1.SelectedRows.Count - 1; i >= 0; i--)
                    {
                        dataGridView1.Rows.Remove(dataGridView1.SelectedRows);
                    }
                }

            }
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            selectedTable = sinfodatebaseDataSet7.Tables[index];
            string queryString = "select * from " + selectedTable.TableName;
            adapter = new SqlDataAdapter(queryString, Properties.Settings.Default.sinfodatebaseConnectionString);
            SqlCommandBuilder builer = new SqlCommandBuilder(adapter);
            adapter.InsertCommand = builer.GetInsertCommand();
            adapter.DeleteCommand = builer.GetDeleteCommand();
            adapter.UpdateCommand = builer.GetUpdateCommand();
            adapter.Fill(selectedTable);
            bindingSource1.DataSource = selectedTable;
            dataGridView1.DataSource = bindingSource1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                bindingSource1.EndEdit();
                adapter.Update(sinfodatebaseDataSet7.Tables[listBox1.SelectedIndex]);
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "保存失败");
            }
        }

作者: 沙子0329    时间: 2014-5-17 14:10
沙子0329 发表于 2014-5-17 14:09
using System;
using System.Collections.Generic;
using System.ComponentModel;

麻烦你啦  先谢谢你呐
作者: xxooxx    时间: 2014-5-19 08:50
我只是路过看看的。




欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4