Reputation: 363
I have a problem binding a DataTable
to a DataGrid
. I have already searched for solutions but just can't get rid of the error. The binding works fine when using WindowsForms, so the DataTable
is correct. I just can't bind it to a WPF-DataGrid.
Error message: AmbiguousMatchException was unhandled
Source: mscorlib
I have already set up new project to get rid of any bad links, etc.
XAML-Code:
<DataGrid x:Name="grid1" Margin="10" ItemsSource="{Binding}"
AutoGenerateColumns="True"></DataGrid>
I have already tried following C#-Code behind:
grid1.ItemsSource = dt.DefaultView;
or
grid1.DataContext = dt.DefaultView;
or
grid1.DataContext = dtex;
Any help is appreciated.
Upvotes: 36
Views: 194115
Reputation: 274
I'd do it this way:
grid1.DataContext = dt.AsEnumerable().Where(x => x < 10).AsEnumerable().CopyToDataTable().AsDataView();
and do whatever query i want between the two AsEnumerable(). If you don't need space for query, you can just do directly this:
grid1.DataContext = dt.AsDataView();
Upvotes: 0
Reputation: 108
You could use DataGrid in WPF
SqlDataAdapter da = new SqlDataAdapter("Select * from Table",con);
DataTable dt = new DataTable("Call Reciept");
da.Fill(dt);
DataGrid dg = new DataGrid();
dg.ItemsSource = dt.DefaultView;
Upvotes: 4
Reputation: 111
In .cs file
grid.DataContext = table.DefaultView;
In xaml file
<DataGrid Name="grid" ItemsSource="{Binding}">
Upvotes: 11
Reputation:
using (SqlCeConnection con = new SqlCeConnection())
{
con.ConnectionString = connectionString;
con.Open();
SqlCeCommand com = new SqlCeCommand("SELECT S_no,Name,Father_Name")
SqlCeDataAdapter sda = new SqlCeDataAdapter(com);
System.Data.DataTable dt = new System.Data.DataTable();
sda.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
dataGrid1.AutoGenerateColumns = true;
dataGrid1.CanUserAddRows = false;
}
Upvotes: 12
Reputation: 183
I'm expecting, as Rohit Vats mentioned in his Comment too, that you have a wrong structure in your DataTable
.
var t = new DataTable();
// create column header
foreach ( string s in identifiders ) {
t.Columns.Add(new DataColumn(s)); // <<=== i'm expecting you don't have defined any DataColumns, haven't you?
}
// Add data to DataTable
for ( int lineNumber = identifierLineNumber; lineNumber < lineCount; lineNumber++ ) {
DataRow newRow = t.NewRow();
for ( int column = 0; column < identifierCount; column++ ) {
newRow[column] = fileContent.ElementAt(lineNumber)[column];
}
t.Rows.Add(newRow);
}
return t.DefaultView;
I have used this DataTable in a ValueConverter
and it works like a charm with the following binding.
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Path=FileContent, Converter={StaticResource dataGridConverter}}" />
So what it does, the ValueConverter
transforms my bounded data (what ever it is, in my case it's a List<string[]>
) into a DataTable
, as the code above shows, and passes this DataTable
to the DataGrid
. With specified data columns the data grid can generate the needed columns and visualize them.
To say it in a nutshell, in my case the binding to a DataTable
works like a charm.
Upvotes: 1
Reputation: 699
In cs file
DataTable employeeData = CreateDataTable();
gridEmployees.DataContext = employeeData.DefaultView;
In xaml file
<DataGrid Name="gridEmployees" ItemsSource="{Binding}">
Upvotes: 69
Reputation: 27
In cs file:
private DataTable _dataTable;
public DataTable DataTable
{
get { return _dataTable; }
set { _dataTable = value; }
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this._dataTable = new DataTable("table");
this._dataTable.Columns.Add("col0");
this._dataTable.Columns.Add("col1");
this._dataTable.Columns.Add("col2");
this._dataTable.Rows.Add("data00", "data01", "data02");
this._dataTable.Rows.Add("data10", "data11", "data22");
this._dataTable.Rows.Add("data20", "data21", "data22");
this.grid1.DataContext = this;
}
In Xaml file:
<DataGrid x:Name="grid1"
Margin="10"
AutoGenerateColumns="True"
ItemsSource="{Binding Path=DataTable, Mode=TwoWay}" />
Upvotes: -2