0

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 tables 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?

Roman
  • 19,581
  • 6
  • 68
  • 84
Evanescence
  • 729
  • 1
  • 10
  • 25
  • If I understand you correctly, your GridView currently shows a list of all students in the DB. Is that correct? Also, what are you doing to display the GridView that you have? e.g. are you autogenerating the columns? – Joel Lee May 08 '11 at 00:56
  • 1
    Can you please post your Code, so that we can better help you. – Muhammad Akhtar May 08 '11 at 00:58
  • too hard to understand your question, please rephrase. If possible attach diagrams; draw them in mspaint or something... – deostroll May 08 '11 at 01:28
  • Sorry if the change I made is a bit drastic, I believe it keeps the meaning the same but is a bit easier to understand. – Roman May 08 '11 at 02:05

1 Answers1

0

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.

Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392