Search code examples
c#wpfdata-bindingwpfdatagrid

trying to display a list of data in a datagrid


I'm trying to display a list of data in a WPF data grid because a textblock just threw the data together without any type of formatting (this is me hoping that formatting of this data is done for me).

This is the code behind

public void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
    try
    {
        if (e == null)
            return;

        if (e.CmsData != null)
        {
            List<string> mylist = new List<string>();
            foreach (var item in e.CmsData.Agents)
            {
                mylist.Add(item.AgName);
                mylist.Add(item.AuxReasonDescription);
            }
            Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
            {
                this.datagrid.ItemsSource = mylist;
            }));
        }
    }
}

The XAML I'm using to populate the data grid

<Grid Height="100" Width="178.201">
    <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Path=mylist, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"   x:Name="datagrid" Background="{x:Null}" Loaded="textBlock_Loaded"/>
</Grid>

this is the agents.cs file that has variables i'm trying to pull into my data grid just incase it is needed.

public class Agent : IEquatable<Agent>
{
    public int Extension { get; set; }
    public int WorkModeDirection { get; set; }
    public string WorkModeDirectionDescription { get; set; }
    public TimeSpan AgTime { get; set; }
    public int AuxReason { get; set; }
    public string AuxReasonDescription { get; set; }
    public int DaInQueue { get; set; }
    public int WorkSkill { get; set; }
    public int OnHold { get; set; }
    public int Acd { get; set; }
    public String LoginId { get; set; }
    public string AgName { get; set; }
    public int EId { get; set; }
    public int Preference { get; set; }
    public DateTime DateTimeCreated { get; set; }
    public DateTime DateTimeUpdated { get; set; }
    public int CmId { get; set; }
    #region Implementation of IEquatable<Agent>

    public bool Equals(Agent other)
    {
        if (ReferenceEquals(null, other))
            return false;

        if (ReferenceEquals(this, other))
            return true;

        return (other.LoginId == LoginId & other.CmId == CmId);
    }

    #endregion

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
            return false;

        if (ReferenceEquals(this, obj))
            return true;

        if (obj.GetType() != typeof(Agent))
            return false;

        return Equals((Agent)obj);
    }

    //public override int GetHashCode()
    //{
    //    return LoginId;
    //}
    public override int GetHashCode()
    {
        string combinedNumber = "" + CmId + LoginId;
        int hash = Convert.ToInt32(combinedNumber);
        return hash;
    }

    public static bool operator ==(Agent left, Agent right)
    {
        return Equals(left, right);
    }

    public static bool operator !=(Agent left, Agent right)
    {
        return !Equals(left, right);
    }

    public override string ToString()
    {
        return " Ag: [Ext:" + Extension + " login:" + LoginId + " AgName:" + AgName + " CmId:" + CmId + "]";
    }
    public bool IsValid()
    {
        return LoginId != null;
    }
}

This is what is currently being displayed...of which contains no information that I am telling it to print out and I have no idea where the numbers are coming from. enter image description here


Solution

  • You should set the ItemsSource of the DataGrid to a List<Agent>:

    public void OnMessageReceived(object sender, MessageReceivedEventArgs e)
    {
        try
        {
            if (e == null)
                return;
    
            if (e.CmsData != null)
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                {
                    this.datagrid.ItemsSource = e.CmsData.Agents.ToList();
                }));
            }
        }
    }
    

    ...and define a column for each property of the Agent class that you want to display in the DataGrid in your XAML markup:

    <DataGrid AutoGenerateColumns="False" 
                          x:Name="datagrid" Background="{x:Null}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="AgName" Binding="{Binding AgName}" />
            <DataGridTextColumn Header="AuxReasonDescription" Binding="{Binding AuxReasonDescription}" />
        </DataGrid.Columns>
    </DataGrid>