c#中dataGridView选择一行在textbox中显示

如题所述

if (textBox1.Text == "")
            {
                MessageBox.Show("铁路局编号不能为空,请选择要修改的铁路局信息", "提示信息", MessageBoxButtons.OK);
                textBox1.Focus();
                return;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("铁路局名称不能为空", "提示信息", MessageBoxButtons.OK);
                textBox2.Focus();
                return;
            }
            SqlConnection conn = CommonFunction.getConn(); //getConn():得到连接对象
            string strCom = "Select * from [铁路局信息表] where 铁路局编号='" + textBox1.Text.Trim() + "'";
            SqlCommand myCommand = new SqlCommand(strCom, conn);
            conn.Open();
            SqlDataReader reader;
            reader = myCommand.ExecuteReader(); //执行command并得到相应的DataReader
            //下面把得到的值赋给tempnote对象
            if (reader.Read())
            {
                reader.Close();
                conn.Close();
            }
            else
            {
                MessageBox.Show("该铁路局编号在数据库中不存在,请先添加后再进行修改", "提示信息", MessageBoxButtons.OK);
                reader.Close();
                conn.Close();
                return;
            }

            SqlConnection conn1 = CommonFunction.getConn(); //getConn():得到连接对象
            conn1.Open();
            //设置SQL语句
            string sqlstr = "UPDATE [铁路局信息表] SET ";
            sqlstr += "铁路局名称='" + textBox2.Text + "' ";
            sqlstr += "where 铁路局编号='" + textBox1.Text.Trim() + "'";
            SqlCommand insertcmd = new SqlCommand(sqlstr, conn1);
            insertcmd.ExecuteNonQuery();
            conn1.Close();
            MessageBox.Show("铁路局信息修改成功!", "提示信息", MessageBoxButtons.OK);
            ReadAllInfo();
            textBox1.Text = "";
            textBox2.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("铁路局编号不能为空,请选择要删除的铁路局信息", "提示信息", MessageBoxButtons.OK);
                textBox1.Focus();
                return;
            }
            SqlConnection conn = CommonFunction.getConn(); //getConn():得到连接对象
            string strCom = "Select * from [铁路局信息表] where 铁路局编号='" + textBox1.Text.Trim() + "'";
            SqlCommand myCommand = new SqlCommand(strCom, conn);
            conn.Open();
            SqlDataReader reader;
            reader = myCommand.ExecuteReader(); //执行command并得到相应的DataReader
            //下面把得到的值赋给tempnote对象
            if (reader.Read())
            {
                reader.Close();
                conn.Close();
            }
            else
            {
                MessageBox.Show("该铁路局编号在数据库中不存在,请检查", "提示信息", MessageBoxButtons.OK);
                reader.Close();
                conn.Close();
                return;
            }

            if (MessageBox.Show("您确定要删除该条铁路局信息吗?", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                SqlConnection conn1 = CommonFunction.getConn(); //getConn():得到连接对象
                conn1.Open();
                string sqlstr1 = "delete from [铁路站信息表] where 铁路局='" + textBox1.Text.Trim() + "'";
                //定义command对象,并执行相应的SQL语句
                SqlCommand myCommand2 = new SqlCommand(sqlstr1, conn1);
                myCommand2.ExecuteNonQuery();
                string sqlstr = "delete from [铁路局信息表] where 铁路局编号='" + textBox1.Text.Trim() + "'";
                //定义command对象,并执行相应的SQL语句
                SqlCommand myCommand1 = new SqlCommand(sqlstr, conn1);
                myCommand1.ExecuteNonQuery();
                conn1.Close();
                textBox1.Text = "";
                textBox2.Text = "";
                ReadAllInfo();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
        }

        private void FrmTLJManage_Load(object sender, EventArgs e)
        {
            ReadAllInfo();
        }

追问

感谢,我事件选错了一直不对

温馨提示:答案为网友推荐,仅供参考
相似回答