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. WPF
  4. try to add data on wpf DataGridColumn

try to add data on wpf DataGridColumn

Scheduled Pinned Locked Moved WPF
wpfcsharphelp
4 Posts 3 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.
  • M Offline
    M Offline
    MemberDotNetting
    wrote on last edited by
    #1

    Hi, I try to import data from an Excel file and display it in a datagrid, the data are email addresses. i want that data will be shown on

    but when i click on boutton, another DataGridColumn appears with data, but i want that data shown on DataGridColumn="Adress" code of parse:

    public class ExcelParser
    {
    public DataTable GetDataTableExcel(string datasource)
    {

            OleDbConnection theConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + datasource + ";Extended Properties=\\"Excel 8.0;HDR=NO;IMEX=1;\\"");
            theConnection.Open();
    
            OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT \* FROM \[Feuil1$\]", theConnection);
    
            DataTable dt = new DataTable();
    
            theDataAdapter.Fill(dt);
    
            return dt;
        }
    

    xaml:

    boutton click excel code behined:

    private void btnExcel_Click(object sender, RoutedEventArgs e)
    {

            wpfOpenfiledialog o = new wpfOpenfiledialog();
            o.openfich(1);
            datasource = o.filename;
    
            ExcelParser exc = new ExcelParser();// class to get data from excel files
            DataTable dt = exc.GetDataTableExcel(datasource);
            //dgvreceipient.ItemsSource = dt.DefaultView;=========>the problem that this ligne will show another column 
          
            
        }
    
    M W 2 Replies Last reply
    0
    • M MemberDotNetting

      Hi, I try to import data from an Excel file and display it in a datagrid, the data are email addresses. i want that data will be shown on

      but when i click on boutton, another DataGridColumn appears with data, but i want that data shown on DataGridColumn="Adress" code of parse:

      public class ExcelParser
      {
      public DataTable GetDataTableExcel(string datasource)
      {

              OleDbConnection theConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + datasource + ";Extended Properties=\\"Excel 8.0;HDR=NO;IMEX=1;\\"");
              theConnection.Open();
      
              OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT \* FROM \[Feuil1$\]", theConnection);
      
              DataTable dt = new DataTable();
      
              theDataAdapter.Fill(dt);
      
              return dt;
          }
      

      xaml:

      boutton click excel code behined:

      private void btnExcel_Click(object sender, RoutedEventArgs e)
      {

              wpfOpenfiledialog o = new wpfOpenfiledialog();
              o.openfich(1);
              datasource = o.filename;
      
              ExcelParser exc = new ExcelParser();// class to get data from excel files
              DataTable dt = exc.GetDataTableExcel(datasource);
              //dgvreceipient.ItemsSource = dt.DefaultView;=========>the problem that this ligne will show another column 
            
              
          }
      
      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Your view binding is to Customers Then you assign the resulting dataset! to the datagrid directly - the data should be put into Customers not the control Check the content of the datatable after it has loaded and make sure the columns are all what you expected. Then put the rows into the customer container, presuming it is an observablecollection

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • M MemberDotNetting

        Hi, I try to import data from an Excel file and display it in a datagrid, the data are email addresses. i want that data will be shown on

        but when i click on boutton, another DataGridColumn appears with data, but i want that data shown on DataGridColumn="Adress" code of parse:

        public class ExcelParser
        {
        public DataTable GetDataTableExcel(string datasource)
        {

                OleDbConnection theConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + datasource + ";Extended Properties=\\"Excel 8.0;HDR=NO;IMEX=1;\\"");
                theConnection.Open();
        
                OleDbDataAdapter theDataAdapter = new OleDbDataAdapter("SELECT \* FROM \[Feuil1$\]", theConnection);
        
                DataTable dt = new DataTable();
        
                theDataAdapter.Fill(dt);
        
                return dt;
            }
        

        xaml:

        boutton click excel code behined:

        private void btnExcel_Click(object sender, RoutedEventArgs e)
        {

                wpfOpenfiledialog o = new wpfOpenfiledialog();
                o.openfich(1);
                datasource = o.filename;
        
                ExcelParser exc = new ExcelParser();// class to get data from excel files
                DataTable dt = exc.GetDataTableExcel(datasource);
                //dgvreceipient.ItemsSource = dt.DefaultView;=========>the problem that this ligne will show another column 
              
                
            }
        
        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        The issue is that you are setting the AutoGenerateColumns property of the DataGrid to true, which will generate it's own columns (one for each field in the datatable) as well as show your template columns (which are not bound to anything by the way, so will not display any results). You have 2 options here:- 1. Forget about your template columns, and leave AutoGenerateColumns on. Then in your SQL query you should only select the columns you want displayed, and use aliases to get the correct headers for your columns i.e.

        SELECT name AS Name, emailaddress AS Address FROM youtable

        . This will give you the results you want except for column width. 2. Turn AutoGenerateColumns off in your DataGrid, and the use the DisplayMemberBinding property of your template columns to bind the column to the datatable field i.e

        <DataGridTemplateColumn Header="Adress" DisplayMemnerBinding={Binding Path=emailaddress} Width="500" IsReadOnly="True">

        . This will also get you your desired result with column widths. Personally I never use DataTables to bind to WPF controls, preferring to have a class that holds my object properties, and then having an ObservableCollection of that class that holds all instances of that class to bind to the DataGrid. Hope this helps

        When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

        M 1 Reply Last reply
        0
        • W Wayne Gaylard

          The issue is that you are setting the AutoGenerateColumns property of the DataGrid to true, which will generate it's own columns (one for each field in the datatable) as well as show your template columns (which are not bound to anything by the way, so will not display any results). You have 2 options here:- 1. Forget about your template columns, and leave AutoGenerateColumns on. Then in your SQL query you should only select the columns you want displayed, and use aliases to get the correct headers for your columns i.e.

          SELECT name AS Name, emailaddress AS Address FROM youtable

          . This will give you the results you want except for column width. 2. Turn AutoGenerateColumns off in your DataGrid, and the use the DisplayMemberBinding property of your template columns to bind the column to the datatable field i.e

          <DataGridTemplateColumn Header="Adress" DisplayMemnerBinding={Binding Path=emailaddress} Width="500" IsReadOnly="True">

          . This will also get you your desired result with column widths. Personally I never use DataTables to bind to WPF controls, preferring to have a class that holds my object properties, and then having an ObservableCollection of that class that holds all instances of that class to bind to the DataGrid. Hope this helps

          When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

          M Offline
          M Offline
          MemberDotNetting
          wrote on last edited by
          #4

          thnk you, please I tried to use interface: this is the code but it doesn't work

          public interface Iimportdata
          {
          DataView GridData { get; set; }
          }

          //traitement2--Excel
          private DataView Data = null;
          public DataView GridData
          {
          get
          { return Data;
          }
          set
          {
          Data = value;
          NotifyPropertyChanged("GridData");
          }
          }

          public event PropertyChangedEventHandler PropertyChanged;

              private void NotifyPropertyChanged(String info)
              {
                  if (PropertyChanged != null)
                  {
                      PropertyChanged(this, new PropertyChangedEventArgs(info));
                  }
              }
          

          private void btnExcel_Click(object sender, RoutedEventArgs e)
          {

                  ExcelParser exc = new ExcelParser();
                  DataTable dt = exc.GetDataTableExcel(datasource);
                
                  Iimportdata imp = Window.GetWindow(this) as Iimportdata;
                  imp.GridData = dt.DefaultView;
                  gbReceipient.Visibility = Visibility.Visible;
                  btnValidate.Visibility = Visibility.Visible;
              }
          

          XAML:

          :(

          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