Click here to Skip to main content
16,012,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using datatable how to add column name manually.


I have gridview as follows

i want to add the following three names manually using datatable in gridview


Course
Batchid
Rate


in Gridview i want the output as follows


Course Batchid Rate
Posted
Comments
Not clear. Please explain again.

You can use DataTable Columns. Add

Reference:
C# DataTable[^]


[Edit member="Tadit"]
Link text added to reflect the article title.
Corrected formatting issues.
[/Edit]
 
Share this answer
 
v2
Try This. Hope it suits you.

ASP.NET
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="false" onrowdatabound="GridView1_RowDataBound" xmlns:asp="#unknown">
     <columns>
          <asp:templatefield headertext="Course">
               <itemtemplate>
               DO Something!
               </itemtemplate>
          </asp:templatefield>
     </columns>
     <columns>
          <asp:templatefield headertext="Batchid">
               <itemtemplate>
               DO Something!
               </itemtemplate>
          </asp:templatefield>
     </columns>
     <columns>
          <asp:templatefield headertext="Rate">
               <itemtemplate>
               DO Something!
               </itemtemplate>
          </asp:templatefield>
     </columns>
</asp:gridview>
 
Share this answer
 
See the reference code.
C#
DataTable Dt = new DataTable();
DataColumn Dc = new DataColumn("Course");
DataColumn Dc1 = new DataColumn("Batchid");
DataColumn Dc2 = new DataColumn("Rate");
Dt.Columns.Add(Dc);
Dt.Columns.Add(Dc1);
Dt.Columns.Add(Dc2);

DataRow dr = Dt.NewRow();
dr["Course"] = "MCA";
dr["Batchid"] = "111";
dr["Rate"] = "A";
Dt.AcceptChanges();
 
Share this answer
 
v2
Example:
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable("mydatatable");
            DataColumn dc = new DataColumn("TextColumn", Type.GetType("System.String"));
            dt.Columns.Add(dc);
            dc = new DataColumn("IntegerColumn", Type.GetType("System.Int32"));
            dt.Columns.Add(dc);
            dc = new DataColumn("DecimalColumn", Type.GetType("System.Decimal"));
            dt.Columns.Add(dc);

            foreach (DataColumn c in dt.Columns)
            { 
                Console.WriteLine(c.ColumnName.ToString());
            }
            Console.ReadKey();
        }
    }
}
 
Share this answer
 
C#
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(int));
table.Columns.Add("Age", typeof(string));
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900