How to add column in datagridview ( C# )

Discussion in 'Programming' started by diepnghitinh, May 21, 2011.

  1. #1
    I have a code :
    
    public DataTable Select_Category()
            {
                dt_category = ds.GetTable("SELECT * FROM itc_category");
                DataTable ndt = new DataTable();
                ndt.Columns.Add("id");
                ndt.Columns.Add("Cat Name");
    
                for (int i = 0; i < dt_category.Rows.Count; ++i)
                {
                    ndt.Rows.Add(new object[] {
                       (i+1).ToString(),
                       dt_category.Rows[i]["cat_name"].ToString()
                   });
                }
                return ndt;
            }
    
    Code (markup):
    with result below:
    I have two columns
    id and Cat Name

    but I would like have 4 columns as shown below
    id , Cat Name , Select , ComboBox

    how to write code for two columns ?
    Select ( checkbox ) and ComboBox

    Please reply me solution.
    thanks :eek:
     
    diepnghitinh, May 21, 2011 IP
  2. echipvina

    echipvina Active Member

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #2
    Easy
    
    public DataTable Select_Category()
            {
                dt_category = ds.GetTable("SELECT * FROM itc_category");
                DataTable ndt = new DataTable();
                ndt.Columns.Add("id");
                ndt.Columns.Add("Cat Name");
                 ndt.Columns.Add("Select");
                ndt.Columns.Add("ComboBox");
                for (int i = 0; i < dt_category.Rows.Count; ++i)
                {
                    ndt.Rows.Add(new object[] {
                       (i+1).ToString(),
                       dt_category.Rows[i]["cat_name"].ToString(),.....
                   });
                }
                return ndt;
            }
    
    Code (markup):
     
    echipvina, May 22, 2011 IP
  3. p.caspian

    p.caspian Peon

    Messages:
    964
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Thats the right answer.
    Thanks echipvina.
     
    p.caspian, May 28, 2011 IP