c# devexpress 中的Gridcontrol 添加行问题

c# devexpress 中的Gridcontrol 添加行问题,第1张

1)向Form1中拖入一个GridControl,两个Button

2)后台代码

using System

using System.Collections.Generic

using System.Linq

using System.Windows.Forms

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        List<Student> studentList 

        int studentId = 1

        public Form1()

        {

            InitializeComponent()

            

            button1.Text = "添加新行"

            button2.Text = "删除选定行"

            BuildDataSource()

        }

        //为gridcontrol1准备数据源

        private void BuildDataSource()

        {

            studentList = new List<Student>()

            studentList.Add(new Student(studentId++)

                 { Name = "张三", Course = "数学", Score = 100 })

            studentList.Add(new Student(studentId++)

                 { Name = "李四", Course = "数学", Score = 90 })

            studentList.Add(new Student(studentId++)

                 { Name = "王五", Course = "数学", Score = 91 })

            //绑定!

            gridControl1.DataSource = studentList

        }

        //添加行

        private void button1_Click(object sender, EventArgs e)

        {

            //添加行,实际上是向数据源(List<Student>集合)添加新的元素

            Student stu = new Student(studentId++) 

                { Name = "钱七", Course = "外语", Score = 34 }

            studentList.Add(stu)

            //向数据源中新加行后,GridControl中自动会添加新行

            gridControl1.RefreshDataSource()

        }

        //删除行

        private void button2_Click(object sender, EventArgs e)

        {

            //获取所有被选行

            int[] rowIds = gridView1.GetSelectedRows()

            if (rowIds.Length == 0) return

            //删除

            foreach (int rowId in rowIds)

            {

                int stuId = (int)gridView1.GetRowCellValue(rowIds[0], "Id")

                Student stu = studentList.First(s => s.Id == stuId)

                studentList.Remove(stu)

            }

            //从数据源中删除行后,GridControl中自动会删除对于的行

            gridControl1.RefreshDataSource()

        }

    }

    //----------------------------------------

    //学生成绩类

    class Student

    {

        public Student(int id)

        {

            Id = id

        }

        //学号

        public int Id { get private set }

        //姓名

        public string Name { get set }

        //课程

        public string Course { get set }

        //成绩

        public float Score { get set }

    }

}

3)可直接在GridControl中修改行,不需要额外编程(除非你想校验输入数据的合法性)

------

总结: 对 Devexpress GridControl中增、删、修改,实际上是对数据源(数据集合)的增、删、修改。也就是说:对数据源修改会"反映"到界面的控件上。

gridview 有点麻烦 可以用repeater

protected void btnCreate_Click(object sender, EventArgs e)

{

try

{

string name = Name.Text

DataTable _dtPerons = buildDt(a,name,dept,date)

Repeater1.DataSource = _dtPerons

Repeater1.DataBind()

}

catch

{

}

}

private DataTable buildDt( string name )

{

DataTable dt = new DataTable()

dt.Columns.Add(new DataColumn("name"))

for (int i = 1i <= numi++)

{

DataRow dr = dt.NewRow()

dr["name"] = name

dt.Rows.Add(dr)

}

return dt

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/7956490.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存