try to add data on wpf DataGridColumn
-
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 }
-
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 }
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 observablecollectionNever underestimate the power of human stupidity RAH
-
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 }
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 yourSQL 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
-
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 yourSQL 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
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:
:(