I have a DataTable where I will store some data that the user informs and through it I want to pass the information to the DataGrid.
gvDados.ItemsSource = dt.DefaultView; //Essa é a ligação entre o DataTable e a Grid
In case the user informs the column name and clicks the AddColumn button, it has the following code:
private void btnAddColuna_Click(object sender, RoutedEventArgs e)
{
bool valida = true;
int nroColunas = dt.Columns.Count;
for (int i = 0; i < nroColunas; i++)
{
if (dt.Columns[i].ToString().ToUpperInvariant().Equals(ttbColuna.Text.ToUpperInvariant()))
valida = false;
}
if (valida)
{
// gvDados.Columns.Clear();
DataGridTextColumn c = new DataGridTextColumn();
c.Header = ttbColuna.Text;
gvDados.Columns.Add(c);
cbbColunas.Items.Add(ttbColuna.Text);
dt.Columns.Add(ttbColuna.Text, typeof(string));
ttbColuna.Text = "";
}
else
MessageBox.Show("Já existe uma coluna com esse nome","Sistema Comercial |B ",MessageBoxButton.OK,MessageBoxImage.Error);
}
With this method it generates the columns, which I want and adds the column in the DataTable and in the DataGrid, is it correct to do this?
Because if I put only the DataTable is not seen in the DataGrid
Let's say that the user added two columns, Size and Color so my DataTable would have to receive
dt.Rows.Add(ttbTamanho.Text, ttbCor.Text);
The information stays in the DataTable but is not displayed in the DataGrid.
Is there any way to put the information in the DataGrid without using the DataTable or some method that causes the information to appear, taking into account that columns and rows are added at runtime