Muhammad Azeem Khan
Muhammad Azeem Khan

Reputation: 91

Property DataGrid.Cloumns is not found on DataGrid Error

i am using wpf on C# in visual studio 2010.Creating DataGrid through sqlserver And wanna set the size of Header my c# code is

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\FUDA\\fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            SqlDataAdapter da = new SqlDataAdapter();
            con.Open();
            DataTable dt = new DataTable();
            da.SelectCommand = new SqlCommand("select * from user_ctrl",con);
            da.Fill(dt);
            dataGrid1.ItemsSource = dt.DefaultView;
        }

And Xaml Code is

<TabControl Height="311" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="503">
            <TabItem Header="tabItem1" Name="tabItem1">
                <Grid>
                    <DataGrid Height="278" HorizontalAlignment="Left" Margin="1,1,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="492" />
                </Grid>
            </TabItem>
        </TabControl>

Above codes works fine but whenever i trying to add in xaml for custom sizing to Header, giving me Error "The attachable property 'Columns' was not found in type 'DataGrid'" Note: i already disable "Auto Generate Column"

  1. Why & How to solve this problem
  2. Also wanna property window in alphabetical way by default (if setting exist), Where & How?

Upvotes: 1

Views: 1907

Answers (1)

dev hedgehog
dev hedgehog

Reputation: 8791

It works for me when I place it like this in xaml:

    <TabControl Height="311" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="503">
        <TabItem Header="tabItem1" Name="tabItem1">
            <Grid>
                <DataGrid Height="278" HorizontalAlignment="Left" Margin="1,1,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="492">
                    <DataGrid.Columns>
                        <DataGridTextColumn Width="100" Header="Column 1" Binding="{Binding}" />
                        <DataGridTextColumn Width="100" Header="Column 2" Binding="{Binding}" />
                        <DataGridTextColumn Width="100" Header="Column 3" Binding="{Binding}" />
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>
        </TabItem>
    </TabControl>

No errors being thrown for me... I hope this helps you any futher :)

Upvotes: 1

Related Questions