首页 > 其他分享 >Form中向DataGridView控件添加数据的三种方式

Form中向DataGridView控件添加数据的三种方式

时间:2022-11-13 11:33:40浏览次数:45  
标签:控件 Form students DataGridView Add Student 泛型 new

1.利用SqlDataAdapter对象向DataGridView中添加数据

using (SqlDataAdapter da = new SqlDataAdapter("select * from Product", DBService.Conn))
{
      DataSet ds = new DataSet();
      da.Fill(ds);
      this.dataGridView1.DataSource = ds.Tables[0];
}

 

2. 利用SqlDataReader填充DataGridView

using (SqlCommand command = new SqlCommand("select * from product", DBService.Conn))
{
      SqlDataReader dr = command.ExecuteReader();
      BindingSource bs = new BindingSource();
      bs.DataSource = dr;
      this.dataGridView1.DataSource = bs;
}

 

3.利用泛型集合向DataGridView中添加数据

        /*第一种泛型示例*/
        //关键代码:(List<>泛型集合)
        private void Form1_Load(object sender, EventArgs e)
        {
            //使用List<>泛型集合填充DataGridView
            List<Student> students = new List<Student>();
            Student hat = new Student("Hathaway", "12", "Male");
            Student peter = new Student("Peter","14","Male");
            Student dell = new Student("Dell","16","Male");
            Student anne = new Student("Anne","19","Female");
            students.Add(hat);
            students.Add(peter);
            students.Add(dell);
            students.Add(anne);
            this.dataGridView1.DataSource = students;
        }
        /*第二种泛型示例*/
        //关键代码:(Dictionary<>泛型集合,与List<>泛型集合略有不同)
        private void Form1_Load(object sender, EventArgs e)
        {
            //使用Dictionary<>泛型集合填充DataGridView
            Dictionary<String, Student> students = new Dictionary<String, Student>();
            Student hat = new Student("Hathaway", "12", "Male");
            Student peter = new Student("Peter","14","Male");
            Student dell = new Student("Dell","16","Male");
            Student anne = new Student("Anne","19","Female");
            students.Add(hat.StuName,hat);
            students.Add(peter.StuName,peter);
            students.Add(dell.StuName,dell);
            students.Add(anne.StuName,anne);
             //在这里必须创建一个BindIngSource对象,用该对象接收Dictionary<>泛型集合的对象
            BindingSource bs = new BindingSource();
             //将泛型集合对象的值赋给BindingSourc对象的数据源
            bs.DataSource = students.Values;
            this.dataGridView1.DataSource = bs;
        }

非原创,做笔记自用

标签:控件,Form,students,DataGridView,Add,Student,泛型,new
From: https://www.cnblogs.com/maojia/p/16885656.html

相关文章

  • uView list 控件分页加载出现抖动问题解决方案
    使用u-list 组件 动态加载数据时 滑动列表元素 会出现抖动的情况解决 设置preLoadScreen为根据page的动态变换就可以了preLoadScreen 列表前后预渲染的屏数,1......
  • WPF 动态加载用户控件
    //这里可以动态加载其他dll文件中的组件Assemblyassem=Assembly.LoadFile($"{Directory.GetCurrentDirectory()}\\{data.DllName}");varonePage=assem.CreateInst......
  • 1001 A+B Format
    1001A+BFormatCalculate a+b andoutputthesuminstandardformat--thatis,thedigitsmustbeseparatedintogroupsofthreebycommas(unlessthereare......
  • Qt 控件学习1 QPushButton
    QPushButton*but=newQPushButton(this);//创建按钮对象but->resize(100,30);//设置大小but->move(200,200);//移动位置but->setText("按钮");//......
  • 自定义的Qt给统计图添加颜色样例控件
    本文主要是给博文“自定义的Qt折线图控件”的图表添加颜色样例。之所以分开叙述是因为本文也是自定义控件,它不仅可以给折线图添加,还可以给其他类型的图表(柱状图、饼形图等,......
  • 自定义的Qt折线图控件
    可以在QWidget内绘制折线图。做的比较简单,因为从写到调试只用了大概1天时间。不太智能,但是可以应对一般的场景。在VS2015和Qt5.9上测试可以使用。当然,由于测试不到位里面可......
  • [FastAPI-03]Form表单
    1.安装依赖pipinstall-ihttp://pypi.douban.com/simple/--trusted-hostpypi.douban.compython-multipart2.表单程序.├──post_test_1.py└──templates......
  • post请求传formdata格式
    constformData=newFormData();fileList.value.forEach(file=>{formData.append('multipartFile',file);});formData.append(......
  • Android开发技巧四--圆角化控件,让它看起来更美
    当需要为应用程序UI控件选择背景的时候,开发者会添加自定义的颜色和形状来代替系统的默认样式,圆角边框看起来是很不错的效果,开发者只需要添加几行代码,就可以在应用程序中使用......
  • 关于定时程序,web/服务/winform
    https://wenda.so.com/q/1371555124068874?src=140&q=c%23%E5%AE%9A%E6%97%B6%E7%A8%8B%E5%BA%8Fhttps://blog.csdn.net/yudehui/article/details/9454947如果是web程序,......