Skip to main content

Microsoft Silverlight

Answered Question Empty DataGrid renders funny when custom columns addedRSS Feed

(0)

SilverlightWebApps
Silverli...

Member

Member

46 points

49 Posts

Empty DataGrid renders funny when custom columns added

I'm working on a demo project that involves a datagrid.  When AutoGenerateColumns is set to true the datagrid looks fine whether ItemsSource is set to null or to real data.  However when I set AutoGenerateColumns to False and define custom columns, the grid only shows a partial border when ItemsSource is set to null.  Once I set ItemsSource to real data it looks fine again.  How can I get the empty grid to render normally -- i.e. have a full border not border on the left hand side and the right hand half of the border blanked out.

 

      <my:DataGrid Margin="8,283,51,87" AutoGenerateColumns="False" x:Name="ResultsGrid" >
        <my:DataGrid.Columns>
                <my:DataGridTextColumn
                    Header="Test Item 1"
                    DisplayMemberBinding="{Binding TestItem1}"/>
                <my:DataGridTextColumn
                    Header="Test Item 2"
                    DisplayMemberBinding="{Binding TestItem2}"/>
        </my:DataGrid.Columns>
      </my:DataGrid>

duefectu
duefectu

Participant

Participant

786 points

241 Posts

Answered Question

Re: Empty DataGrid renders funny when custom columns added

Try setting Width and Height properties for the DataGrid???

Un saludo!
Juan Segura

Plase, "Mark as Answer..." if it solve the problem!

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Re: Empty DataGrid renders funny when custom columns added

Hello, this is by design for Beta 2. But in RTW, the behavior may change, so that even if your data source is empty, as long as you have defined the columns, the columns will be displayed, just there're 0 rows. In Beta 2, the only workaround I can think is to create a fake data source which contains one item, and in the LoadingRow event handler, you hide the first row after data is bound.

private bool fakeData;

List<Data> source = new List<Data>();
source.Add(new Data() { Text = "aaa" });
fakeData = true;
dg.ItemsSource = source;

private void dg_LoadingRow(object sender, DataGridRowEventArgs e)
{
if (fakeData)
{
e.Row.Visibility = Visibility.Collapsed;
}
}

 

Later when you have real data source, you set fakeData to false, or remove the LoadingRow event handler completely.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft

Re: Empty DataGrid renders funny when custom columns added

The partial border is a bug in the Beta 2 template of the DataGrid.  This has been fixed for RTM.  In addition, Yi-Lun is correct, the RTM DataGrid will show the column headers even if you have no items to display.

Yifung Lin [MSFT]

duefectu
duefectu

Participant

Participant

786 points

241 Posts

Re: Re: Empty DataGrid renders funny when custom columns added

I can confirm that, I have the same "bug/effect" in a project that i'm developing now.

Un saludo!
Juan Segura

Plase, "Mark as Answer..." if it solve the problem!

SilverlightWebApps
Silverli...

Member

Member

46 points

49 Posts

Re: Re: Empty DataGrid renders funny when custom columns added

The help you guys gave me above with DataGrid was incorporated into a step-by-step tutorial I wrote on how to build a datacentric silverlight web app that uses WCF and LINQ to submit and retreive data from a database.  You can check it out at my blog at http://http://www.silverlightwebapps.com/Tutorials.aspx.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities