Click here to Skip to main content
15,560,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey there. I've been struggling with developing for a few days now. Caught in a loop, because I'm always tired due to a busy schedule, and not concentrated, so thinking has been difficult. But I have a datagrid I want to programmatically populate. The program signs into a server, and gets a list of game servers. I want to populate my datagrid with information such as ping, and maps, etc, and what gets me is how the code is set up. I wouldn't know how to add all of the information needed for a row. My wording is also pretty terrible, due to lack of rest. Haha. Anywho here's some examples.

This is some of the Launcher.cs Class
C#
        private void ServerListenerName(int id, string name)
{
    Console.WriteLine("server {0} name = {1}", id, name);
                Application.Current.Dispatcher.Invoke((Action)(() =>
                {
                  w.addName(id, name);
                }));

}

private void ServerListenerAttr(int id, string name, string value)
{
    Console.WriteLine("server {0} attr {1} = {2}", id, name, value);
}

private void ServerListenerCap(int id, int cap0, int cap1)
{
    Console.WriteLine("server {0} cap0 = {1} cap1 = {2}", id, cap0, cap1);
}

private void ServerListenerState(int id, int state)
{
    Console.WriteLine("server {0} state = {1}", id, state);

}

private void ServerListenerPlayers(int id, int players)
{
    Console.WriteLine("server {0} players = {1}", id, players);
}

private void ServerListenerAddr(int id, string ip, int port)
{
    Console.WriteLine("server {0} ip = {1} port = {2}", id, ip, port);
}

As you can see, it's broken up into different events. I was hoping to get specific parts of each event into a single string to pass to the window, window being the "w". If you look, all of them have the (int id), so would there be a way to add information to my datagrid's items based on their id? Here's the code for w.addName(id, name);
public void addName(int sID, string sName)
        {
            servl.Items.Add(new zList { zID = sID, zName = sName, zMap = "map", zPlayers = "10/64", zGamemode = "tdm", zPing = 74});
        }

        public class zList
        {
            public int zID { get; set; }
            public string zName { get; set; }
            public string zMap { get; set; }
            public string zPlayers { get; set; }
            public string zGamemode { get; set; }
            public int zPing { get; set; }
        }

Don't mind the temporary strings/integers under the "servl.Items.Add(new zList { zID = sID, zName = sName, zMap = "map", zPlayers = "10/64", zGamemode = "tdm", zPing = 74});", as they are just for general list population.

XAML for the Datagrid
XML
<DataGrid x:Name="servl" HorizontalAlignment="Left" Margin="175,10,0,0" VerticalAlignment="Top" Height="676" Width="1054" FontFamily="Geared Slab">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=zID}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" FontWeight="Bold" FontFamily="Geared Slab" Header="ID" SortDirection="Ascending" CanUserResize="False" Width="40"/>
                <DataGridTextColumn Binding="{Binding Path=zName}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" Header="Name" CanUserResize="False" FontFamily="Geared Slab" Width="250"/>
                <DataGridTextColumn Binding="{Binding Path=zMap}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" Header="Map" CanUserResize="False" Width="130"/>
                <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" Header="Players" CanUserResize="False" Width="85"/>
                <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" Header="Gamemode" CanUserResize="False" Width="135"/>
                <DataGridTextColumn Binding="{x:Null}" ClipboardContentBinding="{x:Null}" Foreground="#FFDADADA" Header="Ping"/>
            </DataGrid.Columns>
        </DataGrid>


So for a TL;DR [Too long, didn't read], I want to add servers to my datagrid, but considering the set up of Launcher.cs's code, I wouldn't know how. Thanks for your time.
Posted

1 solution

You need to bind the ItemsSource to the zList
 
Share this answer
 
Comments
uplusion23 28-May-14 0:22am    
How would I tell the zList what the "zMap" is, for example?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900