Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Setting DataGridView ColumnHeader text

Setting DataGridView ColumnHeader text

Scheduled Pinned Locked Moved C#
questioncsharplinqgraphics
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kanchoette
    wrote on last edited by
    #1

    I am using the following code to import a csv file into a datagridview: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace CSVImporter { public partial class Form1 : Form { public Form1() { InitializeComponent(); List<string[]> csv = parseCSV("C:\\text.csv"); DataTable newTable = new DataTable(); string[] headers = csv[0]; int counter = 0; foreach (string column in headers) { DataColumn colNewDGCol = new DataColumn(); colNewDGCol.ColumnName = "CSV" + headers[counter]; newTable.Columns.Add(colNewDGCol); newTable.Columns[counter].HeaderText = headers[counter]; counter++; } foreach (string[] row in csv) { newTable.Rows.Add(row); } dataGridView1.DataSource = newTable; } public List<string[]> parseCSV(string path) { List<string[]> parsedData = new List<string[]>(); using (StreamReader readFile = new StreamReader(path)) { string line; string[] row; while ((line = readFile.ReadLine()) != null) { row = line.Split(','); parsedData.Add(row); } } return parsedData; } } } But the line: newTable.Columns[counter].HeaderText = headers[counter]; does not work. How can I create the appropriate column header text please?

    S 1 Reply Last reply
    0
    • K kanchoette

      I am using the following code to import a csv file into a datagridview: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace CSVImporter { public partial class Form1 : Form { public Form1() { InitializeComponent(); List<string[]> csv = parseCSV("C:\\text.csv"); DataTable newTable = new DataTable(); string[] headers = csv[0]; int counter = 0; foreach (string column in headers) { DataColumn colNewDGCol = new DataColumn(); colNewDGCol.ColumnName = "CSV" + headers[counter]; newTable.Columns.Add(colNewDGCol); newTable.Columns[counter].HeaderText = headers[counter]; counter++; } foreach (string[] row in csv) { newTable.Rows.Add(row); } dataGridView1.DataSource = newTable; } public List<string[]> parseCSV(string path) { List<string[]> parsedData = new List<string[]>(); using (StreamReader readFile = new StreamReader(path)) { string line; string[] row; while ((line = readFile.ReadLine()) != null) { row = line.Split(','); parsedData.Add(row); } } return parsedData; } } } But the line: newTable.Columns[counter].HeaderText = headers[counter]; does not work. How can I create the appropriate column header text please?

      S Offline
      S Offline
      souidi abderrahman
      wrote on last edited by
      #2

      bonjour, tu dois faire newTable.Columns[counter].ColumnName = headers[counter]; au lieu de newTable.Columns[counter].HeaderText = headers[counter]; je l'ai testé et j'ai pu afficher les données dans grid view

      K 1 Reply Last reply
      0
      • S souidi abderrahman

        bonjour, tu dois faire newTable.Columns[counter].ColumnName = headers[counter]; au lieu de newTable.Columns[counter].HeaderText = headers[counter]; je l'ai testé et j'ai pu afficher les données dans grid view

        K Offline
        K Offline
        kanchoette
        wrote on last edited by
        #3

        Beaucoup de mercis de votre aide. Beaucoup a apprécié.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups