简体   繁体   中英

C# .net Datagrid binding from different sources

I have a problem using Datagrid in C# .net.

I would like to print a two dimensional array in my application and be able to add some columns. I would like to do something like that:

              Student 1      Student 2
Maths    -----------  7  ----------- 5
History  -----------  6  ----------- 8

Then, I would like to be able to add other columns to enter the marks of other students. My problem is that I store the mark in an object witch has a mark field and when I do not succeed to bind each column with each student's marks. Each time I try to do this, I have the same mark on each row.

I tried to do something like that:

BoundColumn nameColumn = new BoundColumn();
nameColumn.DataField = "Mark";
nameColumn.DataFormatString = "{0}";

this.MarksDatagrid.Columns.Add(nameColumn);
this.MarksDatagrid.AutoGenerateColumns = false;

this.MarksDatagrid.DataSource = listToPrint;
this.MarksDatagrid.DataBind();

In listToPrint , I have an object with a student and his mark. Thanks a lot for your help :)

you can bind datagrid like below:

        Maths     History 

Student 1 50 55 Student 2 60 65

Thanks for your answer, and sorry to answer so late. I was not here for a few days...

My problem does not concern binding actually. My problem is to add columns. In my application, I would like to be able to add some school subjects with an add button (it is ok for that) but I also would like to be able to add some students to give them a mark in each school subject.

As I don't know how many students there will be, I cannot add some columns in my asp file and bind them in the .cs file. I would like to add some columns in dynamic way.

I don't know if I was very clear in my explanations...

Thanks for your help ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM