C#winform简单操作

50 篇文章 1 订阅

目录

C#winform在DataGridView控件中加入ComboBox下拉列表框

主窗体实例化添加窗体添加成功后返回主窗体刷新

传过来是字符串 转id

怎么获取winform中动态代码生成的控件的状态

Datagridview获取当前行

Combox绑定数据库

C# winfrom——DataGridView 选中某一行的事件

End


C#winform在DataGridView控件中加入ComboBox下拉列表框


  private void mainForm_Load(object sender, EventArgs e)

        {

            string sqll = "select * from Type";

            List<string> ListData = new List<string> { "张三", "里斯", "王六" };

            DataGridViewComboBoxColumn column1 = new DataGridViewComboBoxColumn();

            column1.Name = "Name";

            column1.DataPropertyName = "Name";//对应数据源的字段

            column1.HeaderText = "combox姓名";

            column1.Width = 80;

            this.dataGridView1.Columns.Add(column1);

            column1.DataSource = ListData;

           //数据库的

            DataGridViewComboBoxColumn column12 = new DataGridViewComboBoxColumn();

            column12.DisplayMember = "Type";

            column12.ValueMember = "TypeID";

            column12.HeaderText = "combox类型";

            column12.Width = 80;

            this.dataGridView1.Columns.Add(column12);

            column12.DataSource = DBHelper.ExecuteTable(sqll);

          //文本的

            //DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();

            //column.Name = "name";

            //column.DataPropertyName = "name";//对应数据源的字段

            //column.HeaderText = "文本类型";

            //column.Width = 80;

            //this.dataGridView1.Columns.Add(column);

            string sql = "select name 法律,t.Type 类型,description 内容 from book g,Type t where t.TypeID=g.TypeID";

            dataGridView1.DataSource = DBHelper.ExecuteTable(sql);

       

            cbx1.DataSource = DBHelper.ExecuteTable(sqll);

            cbx1.DisplayMember = "Type";

            cbx1.ValueMember = "TypeID";

        }

   private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

        {

            ComboBox combo = e.Control as ComboBox;

            if (combo != null)

            {

                combo.SelectedIndexChanged +=

                new EventHandler(ComboBox_SelectedIndexChanged);

            }

        }

        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)

        {

            ComboBox combo = sender as ComboBox;

            string selectedItem = combo.Text;//拿到选择后的值

        }

设置单击下拉模式(否则需要点两下才能显示下拉框):

DataGridView属性 EditMode EditOnEnter  
EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dgv_EditingControlShowing);

主窗体实例化添加窗体添加成功后返回主窗体刷新

    AddForm add = new AddForm();

            //窗口中叠

            if (add.ShowDialog()== DialogResult.OK)

            {

                var sql = " SELECT Sid '序号', SName '姓名', SAddress '学生类别', State '户口所在地', STel '学生电话', Class '班级编号' FROM dbo.tb_stu";

                dataGridView1.DataSource = DBHelper.ExecuteTable(sql);

            }

添加窗体

 

                string sql = string.Format("insert into tb_stu values('{0}','{1}')", textBox1.Text, textBox2.Text);

            if (DBHelper.ExecuteNonQuery(sql))

            {

                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK);

                DialogResult = DialogResult.OK;

                //属性链接

         

            }

            else

            {

                MessageBox.Show("添加不成功", "提示", MessageBoxButtons.OK);

            }
 

传过来是字符串 转id

      public SW(string sid)

        {

            InitializeComponent();

            string sql2 = $"select id from  student where name='{sid}'";

            var b1 = dbhelper.GetDataTable(sql2).Rows[0][0];

            Sid = Convert.ToInt32(b1);

          //  var a = dbhelper.GetDataTable("select name from student where id=" + Sid);

           // var b = a.Rows[0]["name"];

          //  lbl3.Content = "欢迎您" + sid;

            string sql = "select * from course";

            dd.ItemsSource = dbhelper.GetDataTable(sql).DefaultView;

        }

怎么获取winform中动态代码生成的控件的状态

https://blog.csdn.net/qq_28821897/article/details/130667945

遇到的困难

在renewButton_Click方法中,无法调用生成控件的状态,只有在InitializeComponents方法中可以使用

解决思路

1,首先想到的是在InitializeComponents中把控件的属性赋值出去给变量,然后在其他地方调用

InitializeComponents只初始化刚刚生成的时候,后面调用的变量为空值不行

2.renewButton_Click方法放入InitializeComponents中,private删除,即可调用,不影响,成功解决

Datagridview获取当前行

private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            /*int index = dataGridView1.CurrentCell.RowIndex;   */      //获取被选中汽车编号

                                                                        //if (this.dataGridView1.Rows.Count>0)

                                                                        //右键单击修改

            if (dataGridView1.CurrentRow!=null)

            { 

                var sql = "select * from Type";

                DataTable table=new DataTable();

                table = DBHelper.GetDataTable(sql);

                cbxType.DataSource = table;

                cbxType.DisplayMember = "TypeName";

                cbxType.ValueMember = "TypeID";   //comboBox汽车类型数据绑定

            

                cmbCarID.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();

                /*    cmbCarID.Text = Convert.ToString(DBHelper.GetDataTable(sql).Rows[index]["CarID"]);*///汽车编号显示绑定

                tbxCarName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();

                //tbxCarName.Text= Convert.ToString(DBHelper.GetDataTable(sql).Rows[index]["CarName"]);

                tbtCarPrice.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();

                //tbtCarPrice.Text= Convert.ToString(DBHelper.GetDataTable(sql).Rows[index]["Price"]);

                tbxCarDecription.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();

                //tbxCarDecription.Text= Convert.ToString(DBHelper.GetDataTable(sql).Rows[index]["Description"]);

                cbxType.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();

            }

        }

Combox绑定数据库

  cbx1.DataSource = DBHelper.ExecuteTable(sqll);

            cbx1.DisplayMember = "Type";

            cbx1.ValueMember = "TypeID";
 

C# winfrom——DataGridView 选中某一行的事件

先将SelectionMode属性设置一下,改为fullrowselection. 然后给一个cellclick事件 注意:点击表头时也会触发此事件,在取值时要排除

private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)

{

  //当点击表头部的列时,e.RowIndex==-1

  if (e.RowIndex > -1)

  {

    this.txtUsername.Text = this.dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString();

    this.txtPassword.Text = this.dataGridView2.Rows[e.RowIndex].Cells[2].Value.ToString();

    this.txtPosition.Text = this.dataGridView2.Rows[e.RowIndex].Cells[3].Value.ToString();

    this.txtStatus.Text = this.dataGridView2.Rows[e.RowIndex].Cells[6].Value.ToString();

    this.txtName.Text = this.dataGridView2.Rows[e.RowIndex].Cells[7].Value.ToString();

  }

}

Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation
链接Mysql的

End

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星尘库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值