C
C#3y ago
idris_2020

✅ How can I replace a value in a 2D array, then refresh a datagridview with the new array

int[,] seats =
{
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
};

Console.WriteLine("Enter row:");
int rowBooking = int.Parse(Console.ReadLine());

Console.WriteLine("Enter column:");
int columnBooking = int.Parse(Console.ReadLine());

seats[rowBooking-1, columnBooking-1] = 1;

Console.Write(" ");

for (int columnNumber = 1; columnNumber <= 6; columnNumber++)
{
Console.Write(columnNumber + " ");
}

int rowNumber = 1;

static void rowDividers()
{
Console.WriteLine();
Console.WriteLine(" - - - - - - - - - - - - -");
}

rowDividers();

for (int x = 0; x < seats.GetLength(0); x++)
{
Console.Write(rowNumber++ + " ");
Console.Write("| ");

for (int y = 0; y < seats.GetLength(1); y++)
{
Console.Write("{0} ", seats[x, y]);
Console.Write("| ");
}

rowDividers();
}
int[,] seats =
{
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0},
};

Console.WriteLine("Enter row:");
int rowBooking = int.Parse(Console.ReadLine());

Console.WriteLine("Enter column:");
int columnBooking = int.Parse(Console.ReadLine());

seats[rowBooking-1, columnBooking-1] = 1;

Console.Write(" ");

for (int columnNumber = 1; columnNumber <= 6; columnNumber++)
{
Console.Write(columnNumber + " ");
}

int rowNumber = 1;

static void rowDividers()
{
Console.WriteLine();
Console.WriteLine(" - - - - - - - - - - - - -");
}

rowDividers();

for (int x = 0; x < seats.GetLength(0); x++)
{
Console.Write(rowNumber++ + " ");
Console.Write("| ");

for (int y = 0; y < seats.GetLength(1); y++)
{
Console.Write("{0} ", seats[x, y]);
Console.Write("| ");
}

rowDividers();
}
14 Replies
idris_2020
idris_2020OP3y ago
here is the console version for the replacing a data in the 2d array
public void dataSeatsRefresh()
{
for (int x = 0; x < seats.GetLength(0); x++)
{
string[] seat = new string[seats.GetLength(1)];

for (int y = 0; y < seats.GetLength(1); y++)
{
seat[y] = seats[x, y];
}

dtaSeats.Rows.Add(seat);
}
}
public void dataSeatsRefresh()
{
for (int x = 0; x < seats.GetLength(0); x++)
{
string[] seat = new string[seats.GetLength(1)];

for (int y = 0; y < seats.GetLength(1); y++)
{
seat[y] = seats[x, y];
}

dtaSeats.Rows.Add(seat);
}
}
this is the refreshing the datagridview when on load it displays the array but when i replace a value and then call it it doesnt resfresh
idris_2020
idris_2020OP3y ago
the console version works perfectly fine
idris_2020
idris_2020OP3y ago
????
Pendramon
Pendramon3y ago
I haven't used WinForms in a while and have definitely not used data grids so I don't know how the data is binded but at a quick glance it sounds to me like you need to implement INotifyPropertyChanged and call the property changed event when you change the data so that it notifies the UI that the data has changed and it needs to update. Again, it might be a problem in how you are setting the data inside the data grid but if your data grid has a binding to the data I'd say the most probable suspect is not calling the PropertyChanged event from INotifyPropertyChanged.
idris_2020
idris_2020OP3y ago
i mean it loads the initial array right it just doesnt change when the array does
Pendramon
Pendramon3y ago
Yes that is exactly the cause of not calling the PropertyChanged event.
idris_2020
idris_2020OP3y ago
but i can see from the console the array changes its just that the datagridview not changing
Pendramon
Pendramon3y ago
Because that event tells the UI that the initial data has changed and it needs to update the view to show that data change.
idris_2020
idris_2020OP3y ago
okay could you give a short example of using the INotifyPropertyChanged
idris_2020
idris_2020OP3y ago
aight ill try this ty
Pendramon
Pendramon3y ago
The example code is too big and can confuse you, focus on INotifyPropertyChanged, PropertyChanged event and the NotifyPropertyChanged method That is what you need to implement, and then call NotifyPropertyChanged method every place you change the data or better yet if your data is stored in a property to just call it in the set accessor
Accord
Accord3y ago
Closed!
idris_2020
idris_2020OP3y ago
ty

Did you find this page helpful?