0
I have a Datatable where I will store some data that the user informs and through it I want to pass the information to 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 button Addircoluna, which 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 Datatable and Datagrid, it is correct to do this?
Because if I just put it on Datatable it is not visualized on Datagrid
Until then everything is ok, but when I add a line in Datatable so that it can appear in Datagrid does not work, a line appears but blank without the data ...
Let’s say the user added two columns, Size and Color so my Datatable would have to receive
dt.Rows.Add(ttbTamanho.Text, ttbCor.Text);
The information is in the Datatable, but is not shown in the Datagrid.
Is there any way to put the information in the Datagrid without using the Datatable or any method that makes the information appear, taking into account that columns and rows are added at runtime
Thank you your reply was very helpful and managed to help me! if I could also help with that thank you: http://answall.com/questions/183435/defini%C3%A7%C3%A3o-de-row-header-de-um-datagrid-atrav%C3%A9s-das-linhas-de-uma-coluna-de-um-da
– Bruno