0

I'm having two classes Cricket, Football and List of Observable collections of type object. Based on certain condition I want to add the object of type Cricket/Football to Observable Collection. I'm not assigning any data i.e just creating and instance of class Cricket/Football and adding that instance to Observable Collections and binding to UI. My expectation is, as I'm not assigning any data to the instance of Cricket/Football, only header has to create in the datagrid. But what I found was a row with the default value of the variables defined under the respective class along with the row header as I'm creating the instance of that class. How shall I avoid creating void row where my datagrid header is unaffected.

<DataGrid SelectionMode="Single" VerticalAlignment="Stretch" ItemsSource="{Binding itemSource, UpdateSourceTrigger=PropertyChanged}"  CanUserReorderColumns="False" CanUserAddRows="False" IsReadOnly="True" CanUserDeleteRows="False" CanUserResizeColumns="False" HorizontalGridLinesBrush="Black" VerticalGridLinesBrush="Black" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" />

Edit

5
  • You need to provide more code to better understand your issue. Commented Jul 25, 2016 at 15:47
  • I understand Ayyapppan. I was unable to post full question as I was getting some error while submitting the question. Commented Jul 26, 2016 at 4:38
  • Ayyapppan, as I was unable to add complete question, I have added the image of the rest. Go to edit link. Thank You. Commented Jul 26, 2016 at 4:51
  • If your OC contains some element, then it will be used by DataGrid, your variables will of course contain default values. Commented Jul 26, 2016 at 5:09
  • I understand Anjum. Isn't there any alternative where I can just avoid binding the default row to DataGrid. If that is not at all possible, is there any way where I can just remove that default row from DataGrid after its been binded. Commented Jul 26, 2016 at 5:32

1 Answer 1

1

I think your datagrid's CanUserAddRows property is set to true. Just set it to false to fix your issue.

CanUserAddRows="false"
IsReadOnly="True"

EDIT : Sorry. I read your question properly right now. It’s because the ObservableCollection’s type is object. I will show you a small sample so that you understand how binding works in a datagrid. Your collection should have public properties, so datagrid could bind columns to it. If you use collection type of Object than you have no properies for binding, so the empty rows will be displayed.

XAML :.

<DataGrid AutoGenerateColumns="False" Height="253" HorizontalAlignment="Left" Margin="27,24,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="448">
            <DataGrid.Columns>
                <DataGridTextColumn Header="First" Binding="{Binding Path=Field, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            </DataGrid.Columns>
 </DataGrid>

Code behind :

public partial class MainWindow : Window 
{ 
    public ObservableCollection dataSource;
    public MainWindow()
    {
        InitializeComponent();

        this.dataSource = new ObservableCollection<SomeDataSource>();

        this.dataSource.Add(new SomeDataSource { Field = "123" });
        this.dataSource.Add(new SomeDataSource { Field = "1234" });
        this.dataSource.Add(new SomeDataSource { Field = "12345" });

        this.dataGrid1.ItemsSource = this.dataSource;
    }
}

public class SomeDataSource
{
    public string Field {get;set;}
}
Sign up to request clarification or add additional context in comments.

8 Comments

Please have a look at the xaml which I have posted in the question. I have already tried that. When I create an instance of a class and add that instance to Observable Collection list and bind that list to DataGrid, I'm getting a default row created which holds the default value of the variables declared in that class. I understand that this behavior is natural and I cannot override it. So is there any methodology where I can remove that default row so that my datagrid header is not effected.
Firstly, I'm not creating static column using DataGrid.Columns. I just want DataGrid to take the definition of the class which I specify i.e it just has to bind the property defined in the class to the DataGrid Header and not to create the default row with the default value of the property in that class.
@SharanRM: You can use the dataclass as per your requirement. I just specified an example. The blank row is added since you’re using ObservableCollection of type object. Please replace that with data classes as mentioned in the example and it should work. Hope you got the point.
Whatever you suggested, works totally fine if I know the type of the Observable Collection to be defined. In my scenario, I have two classes and based on some condition I will be creating the object of the class which I wish to present in the DataGrid and then adding that object to Observable Collection list of type object. I will only get to know at the runtime of what type of object I need to create.
That I understood. But you're initially binding the source of Datagrid to an Observable Collection of type object right? That is causing the issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.