开发者

Edit the code of a GridView column asp.net

开发者 https://www.devze.com 2023-03-04 20:05 出处:网络
I\'ve been looking for a way to control the text that appears in a particular column of a GridView. For example consider a database with two ta开发者_开发技巧bles Student and Class.

I've been looking for a way to control the text that appears in a particular column of a GridView.

For example consider a database with two ta开发者_开发技巧bles Student and Class.

  1. I want to add a column to the GridView which print out all the students in the Database, the column will show the student's class name, how can do it? (I can normally print the ClassId since its a FK in the student table)

  2. I want to add a column to the GridView which print all the classes, the column will count the number of students in each class, how can I do it?


1- You can do that simply with inner join or a stored procedure to get the class name beside all the data you need in your query.

2- More than one way to do what you want:

For example: you can add a column to your data table (empty column ), and fill it later through using Sum() aggregate function in a query.

        DataTable result_dt = DAL_Helper.Return_DataTable(sqlSelect);//your original query
        result_dt.Columns.Add("NumberOfStudent");
        result_dt.Columns["NumberOfStudent"].DataType = typeof(string);
        result_dt.Columns["NumberOfStudent"].MaxLength = 255;

        if (result_dt.Rows.Count > 0)
        {
            for (int i = 0; i < result_dt.Rows.Count; i++)
            {
               //Here u can fill your new empty column.
            }
        }
        result_dt.AcceptChanges();

After you return your customized data table(as a data source), you can bind it to the grid view.

Another solution : add an empty column to the grid view and in the RowDataBound event of the grid view you can fill this column through some loop and you can use LINQ to help you in getting the summation.

0

精彩评论

暂无评论...
验证码 换一张
取 消