0

i made a sudoku game using windows forms, the datagrid was very easy to handle, for example

dataGridView1[2,3]=5;

now in wpf the datagrid cant be used like this and i need to find the easiest way to test and fill the grid, for example i had codes like :

for (i = 0; i < 9; i++)
            {
                for (j = 0; j < 8; j++)
                {
                    for (k = j + 1; k < 9; k++)
                    {
                        if (dataGridView1[i, k].Value != null)....

                        }
                    }
                }
            }

any help?

1 Answer 1

1

I would not read the content of the dataGridView. Instead I would bind a control (such as the datagridview) to a data structure that represents the values in the Sudoku.

This would allow you to simply check the data structure (even add methods, properties and events to it)

Just make a Grid (9x9 cells) and a textblock in each cell and bind the text property of each cell to an element of a two dimensional array:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        ...
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        ...
    </Grid.RowDefinitions>
    <TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding Path=Cells[0][0]}"/>
    <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Path=Cells[1][0]}"/>
    <TextBlock Grid.Column="2" Grid.Row="0" Text="{Binding Path=Cells[2][0]}"/>
    ...
</Grid>

And set the Grid's DataContext in code:

class Sudoku
{
    public int[,] Cells = new int[9,9];
    ...
}


private Sudoku _sudoku;

MainWindow_Loaded(...)
{
    _sudoku = new Sudoku();
    grid.DataContext= _sudoku;

}

Now you can simply set and check _sudoku.Cells[2,3]

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the help! anw in xaml section, Binding Path=Cells[0,0] won't work. it must be Binding Path=Cells[0][0]

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.