'How can we match or join headerdatecolumns(Horizontal) with sql table(Vertical) Wpf
below code not matching the Headerdatecolumns with sql inam1(Table) Indate Column, I am not able to fix this problem someone help me to fix this problem
this screenshot is the datatable
this screenshot is inam(table)
this screenshot is giving result

Class MainWindow
Dim connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\BUTTONFORMWPF1 - COPY\BUTTONFORMWPF1\NEWDATABASE.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True")
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
Dim table As DataTable = New DataTable("record")
Dim col = New DataGridTextColumn()
col.Header = "Column1"
col.Binding = New Binding("[0]")
dg.Columns.Add(col)
col = New DataGridTextColumn()
col.Header = "Column2"
col.Binding = New Binding("[1]")
dg.Columns.Add(col)
col = New DataGridTextColumn()
Dim startDate As Date = CType(dtpStartDate.SelectedDate, DateTime)
Dim endDate As Date = CType(dtpEndDate.SelectedDate, DateTime)
While startDate <= endDate
Dim NewColumn As New DataGridTextColumn()
NewColumn.Header = startDate.ToShortDateString()
NewColumn.Binding = New Binding("[2]")
dg.Columns.Add(NewColumn)
startDate = startDate.AddDays(1)
End While
Dim rows As List(Of Object) = New List(Of Object)()
Dim value As String()
value = New String(2) {}
value(0) = "ID"
value(1) = "Name"
value(2) = "AM"
rows.Add(value)
Dim sda As SqlDataAdapter = New SqlDataAdapter("Select datatest1.ID, datatest1.Name, inam1.AM from datatest1 inner join inam1 On datatest1.ID=inam1.ID", connection)
For i As Integer = 0 To table.Columns.Count
sda.Fill(table)
rows.Add(table(i))
Next i
dg.ItemsSource = rows
End Sub
End Class
below is the xaml
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:project1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid Name="dg" CanUserAddRows="True"
AutoGenerateColumns="False"
ItemsSource="{Binding}" Margin="293,153,122,120" >
</DataGrid>
<DatePicker x:Name="dtpStartDate" Grid.Row="0" HorizontalAlignment="Left" Height="24" Margin="53,177,0,0" VerticalAlignment="Top" Width="127"/>
<DatePicker x:Name="dtpEndDate" HorizontalAlignment="Left" Height="25" Margin="47,241,0,0" VerticalAlignment="Top" Width="139"/>
<Button x:Name="Button" Click="Button_Click_1" Content="Button" HorizontalAlignment="Left" Height="26" Margin="78,310,0,0" VerticalAlignment="Top" Width="110"/>
</Grid>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
