回 帖 发 新 帖 刷新版面

主题:[原创]C#datagridview删除问题




namespace mis
{
    public partial class Bill : Form
    {
        private DBHelper link = new DBHelper();
        private DataTable newtable;
        SqlDataAdapter da;
        DataSet ds = new DataSet();   
        public Bill()
        {
            InitializeComponent();
        }

        private void labrq_Click(object sender, EventArgs e)
        {

        }

        private void Bill_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“misDataSet1.td”中。您可以根据需要移动或移除它。
            //this.tdTableAdapter.Fill(this.misDataSet1.td);          
            DataGridStateControl();
            takenum tdh = new takenum();
            this.txttdh.Text = tdh.tb_ThGoodID();
            this.labjsr.Text = Userhelper.user;
            //设置单元格格式
            foreach (DataGridViewColumn c in dataGridView1.Columns)
            {
                if (c.Index == 1)
                {
                    c.ReadOnly = true;                 
                }
                if (c.Index == 2)
                {
                    c.ReadOnly = true;
                }
                if (c.Index == 6)
                {
                    c.Visible = false;
                }
            }            
         
        }
       
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.labddrq.Text = DateTime.Now.ToString();
        }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            DBHelper.con.Open();
            string sql = string.Format("select 顾客名称 from customer where 顾客编号 like" + " " + "'" + this.textgkbh.Text.Trim() + "%" + "'");
            SqlCommand cmd = new SqlCommand(sql, DBHelper.con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    this.labgkmc.Text =dr["顾客名称"].ToString();
                }
            }
            else
            {
                MessageBox.Show("顾客表中没有该顾客,请添加新顾客!!");
                Inftake.khbh = this.textgkbh.Text;
                this.textgkbh.Focus();

            }
            dr.Close();

        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
        finally
        {
            DBHelper.con.Close();
        }
    }

        private void btss_Click(object sender, EventArgs e)
        {
            //查顾客信息
            Customernamesearch frm = new Customernamesearch();
            frm.ShowDialog();
            this.textgkbh.Text = Inftake.gkbh;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        int count = 0;
        private void shubiao()
        {           
        int row = this.dataGridView1.CurrentCell.RowIndex ;         
        button1.Location = new Point(button1.Location.X, button1.Location.Y + (row - count) * 23);
        count = row;
        button1.Visible = false;
        if (this.dataGridView1.CurrentCell == this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0])
        {
            button1.Visible = true;
        }
        else
        {
            button1.Visible = false;
        }
                  
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
           
            //查商品信息
            productnamesearch frm = new productnamesearch();
            frm.ShowDialog();
            this.dataGridView1[0,dataGridView1.CurrentCell.RowIndex].Value  = Inftake.spbh;
        }

        private void button2_Click(object sender, EventArgs e)
        {
           //selectWareMessage();          
            //this.dataGridView1.CurrentCell = this.dataGridView1[0, this.dataGridView1.CurrentCell.RowIndex];
            //newtable.Rows.Add(newtable.NewRow());
            DataRow dr = newtable.NewRow();
            newtable.Rows.InsertAt(dr, 1);
        }

        private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
        {
            selectWareMessage();  
            this.tetzje.Text =this.dataGridView1[6,0].Value.ToString ();
            shubiao();
           
        }

        private void selectWareMessage()
        {
        int row=0;
        if (this.dataGridView1.CurrentCell.RowIndex>0)
            {
                row=this.dataGridView1.CurrentCell.RowIndex;
            }
        if (this.dataGridView1[ 0,row].Value.ToString().Trim () != "")
        {  
            try
            {
                DBHelper.con.Open();
                string sql = string.Format("select 商品编号,商品名称,规格 from product where 商品编号 like" + " " + "'" + this.dataGridView1[0,row].Value.ToString ().Trim ()+ "%" + "'");              
                DataTable dt=link .SelectDataBase (sql);               
                if (dt.Rows .Count > 0)
                {
                    this.dataGridView1[0, this.dataGridView1.CurrentCell.RowIndex].Value = dt.Rows[0][0].ToString().Trim();
                    this.dataGridView1[1, this.dataGridView1.CurrentCell.RowIndex].Value = dt.Rows[0][1].ToString().Trim();
                    this.dataGridView1[2, this.dataGridView1.CurrentCell.RowIndex].Value = dt.Rows[0][2].ToString().Trim();              
                      
                }

                else
                {
                    
                    DBHelper.con.Close();
                    MessageBox.Show("没有该商品,请选择其它商品!!", "提示");
                    this.dataGridView1[1, this.dataGridView1.CurrentCell.RowIndex].Value ="";
                    this.dataGridView1[2, this.dataGridView1.CurrentCell.RowIndex].Value = "";

                }           

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.con.Close();
            }               
        }
        }
        public void DataGridStateControl()
        {
            newtable = new DataTable();
            newtable.Columns.Add("商品编号", typeof(string));
            newtable.Columns.Add("商品名称", typeof(string));
            newtable.Columns.Add("规格型号", typeof(string));
            newtable.Columns.Add("售价", typeof(int));
            newtable.Columns.Add("数量", typeof(int));
            newtable.Columns.Add("总额", typeof(int),"数量*售价");
            newtable.Columns.Add("总金额", typeof(int), "sum(总额)");         
            this.dataGridView1.DataSource = newtable;
            newtable.Rows.Add(newtable.NewRow());
               
        }
     
        private void button3_Click(object sender, EventArgs e)
        {//保存数据
            try
            {
                int result=0;
                DBHelper.con.Open();
                for (int row = 0; row < this.newtable.Rows.Count; row++)
                {
                  string sq = string.Format("INSERT INTO td (提单号, 日期, 顾客编号, 顾客姓名, 商品编号, 商品名称, 规格, 单价, 数量, 总额,经手人) VALUES ( '" + this.txttdh.Text + "' ,'" + this.labddrq.Text + "','" + this.textgkbh.Text + "' ,'" + this.labgkmc.Text + "','" + this.dataGridView1.Rows[row].Cells[0].Value + "','" + this.dataGridView1.Rows[row].Cells[1].Value + "','" + this.dataGridView1.Rows[row].Cells[2].Value + "','" + this.dataGridView1.Rows[row].Cells[3].Value + "','" + this.dataGridView1.Rows[row].Cells[4].Value + "','" + this.dataGridView1.Rows[row].Cells[5].Value + "','" + this.labjsr.Text + "')");
                    SqlCommand cmd = new SqlCommand(sq, DBHelper.con);
                    int a = cmd.ExecuteNonQuery();
                    if (a >0)
                    {
                        result++;
                    }
                    a = 0;
                }
                if (result == this.newtable.Rows.Count)
                    {
                        MessageBox.Show("保存成功!!");
                    }
                    else
                    {
                        MessageBox.Show("保存失败!!");
                    }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.con.Close();
            }
            //newtable.Clear();
            //newtable.Rows.Add(newtable.NewRow());

        }

        private void button4_Click(object sender, EventArgs e)
        {//删除一行数据
            try
            { 
                                
                if (this.dataGridView1.CurrentCell.RowIndex > 0 )
                {
                    int row = this.dataGridView1.CurrentCell.RowIndex;                   
                    this.dataGridView1.Rows.RemoveAt(row);
                }
                if (this.dataGridView1.CurrentCell.RowIndex <= 0)
                {
                    //newtable.Rows.Add(newtable.NewRow());
                    for (int i = 0; i <3; i++)
                    {
                        this.dataGridView1[i ,0].Value = "";
                    }
                    this.dataGridView1[3, 0].Value =0;
                    this.dataGridView1[4, 0].Value = 0;
                    //DataRow dr = newtable.NewRow();
                    //newtable.Rows.InsertAt(dr, 0);
                }
              
            }
            catch (Exception ex)
            {
                
                MessageBox.Show (ex.Message );
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            tdreport frm = new tdreport();
            frm.ShowDialog();
        }
   
       
    }


    }


回复列表 (共1个回复)

沙发

//为什么我在删除时只能从后面删除而不能删除中间的,如果删除会出现红色部分
private void selectWareMessage()
        {
        int row=0;
       [color=FF0000] if (this.dataGridView1.CurrentCell.RowIndex>0)//提示错误:没有实例化[/color]            {
                row=this.dataGridView1.CurrentCell.RowIndex;
            }
        if (this.dataGridView1[ 0,row].Value.ToString().Trim () != "")
        {  
            try
            {
                DBHelper.con.Open();
                string sql = string.Format("select 商品编号,商品名称,规格 from product where 商品编号 like" + " " + "'" + this.dataGridView1[0,row].Value.ToString ().Trim ()+ "%" + "'");              
                DataTable dt=link .SelectDataBase (sql);               
                if (dt.Rows .Count > 0)
                {
                    this.dataGridView1[0, this.dataGridView1.CurrentCell.RowIndex].Value = dt.Rows[0][0].ToString().Trim();
                    this.dataGridView1[1, this.dataGridView1.CurrentCell.RowIndex].Value = dt.Rows[0][1].ToString().Trim();
                    this.dataGridView1[2, this.dataGridView1.CurrentCell.RowIndex].Value = dt.Rows[0][2].ToString().Trim();              
                      
                }

                else
                {
                    
                    DBHelper.con.Close();
                    MessageBox.Show("没有该商品,请选择其它商品!!", "提示");
                    this.dataGridView1[1, this.dataGridView1.CurrentCell.RowIndex].Value ="";
                    this.dataGridView1[2, this.dataGridView1.CurrentCell.RowIndex].Value = "";

                }           

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBHelper.con.Close();
            }               
        }
        }

我来回复

您尚未登录,请登录后再回复。点此登录或注册