4

I need to insert values in a table that have different columns from time to time. The columns and row data are updated from MySql. Each row values are in single MySql cells with the following format:

ColumnName{Delimiter}Value{BigDelimiter}Column2Name{Delimiter}Value2...

So I split the cell strings to get the column header and value, as the user can rearrange columns, modify, delete or insert new ones. I searched for a solution, though I get nothing but empty rows:

    private void GetDataTableValues()
    {
        if (dtData.Value != null)
        {
            try
            {
                LoadFields();
                dgwDataMain.Items.Clear();
                dgwDataMain.Columns.Clear();
                foreach (Fields field in fields)
                {
                    DataGridTextColumn column = new DataGridTextColumn();
                    column.Header = field.name;
                    column.Binding = new Binding(field.name);
                    dgwDataMain.Columns.Add(column);
                }

                if (connection.State == System.Data.ConnectionState.Broken || connection.State == System.Data.ConnectionState.Closed)
                    connection.Open();

                command.Parameters.Clear();
                DateTime dt = dtData.Value ?? DateTime.Now;
                command.Parameters.Add("@date", MySqlDbType.Date, 50).Value = dt.ToString("yyyy-MM-dd");
                command.CommandText = "SELECT value,team FROM sessions WHERE date=@date";

                List<string> teams = new List<string>();
                foreach (Control ctrl in panDataFilter.Children)
                    if ((ctrl as CheckBox).IsChecked == true)
                        teams.Add(Convert.ToString((ctrl as CheckBox).Content));

                using (MySqlDataReader reader = command.ExecuteReader())
                {

                    while (reader.Read())
                    {
                        bool v = false;
                        if (teams.Contains(reader[1].ToString()) || teams.Count == 0)
                            v = true;
                        if (v)
                        {
                            DatabaseObject obj = new DatabaseObject();
                            List<string> str2 = new List<string>(reader[0].ToString().Split(new string[] { "</s&p>" }, StringSplitOptions.None).ToList());
                            obj.Items = new List<string>(str2.Count);
                            foreach (string str in str2)
                            {
                                List<string> item = new List<string>(str.Split(new string[] { "<&p>" }, StringSplitOptions.None).ToList());
                                int index = dgwDataMain.Columns.Single(c => c.Header.ToString() == item[0].ToString()).DisplayIndex;
                                obj.Items.Insert(index, item[1].ToString());
                            }
                            dgwDataMain.Items.Add(obj);
                        }
                    }
                }
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.ErrorCode.ToString() + ": " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }
    }

    public class DatabaseObject
    {
        public List<string> Items = new List<string>();
    }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
ice 13
  • 333
  • 4
  • 17
  • Have u tried using a datatable ? – Nicolas Pierre Jan 07 '13 at 12:40
  • I hate using DataTable, as every time I tried using it I failed in C#. I am new to WPF and I am still learning. – ice 13 Jan 07 '13 at 14:31
  • 1
    This SO post should answer your question: [Binding DataGrid to ObservableCollection](http://stackoverflow.com/questions/14171098/binding-datagrid-to-observablecollectiondictionary) – Sphinxxx Jan 07 '13 at 19:20

2 Answers2

0

Please use Observablecollection to bind the data grid. By using observablecollection easily you can add or delete item and not required to reset the data source of data grid.

Sample Code:

observableCollection myClass = new observableCollection(); myClass.add(Class)

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
Nitheen Rao
  • 210
  • 2
  • 11
0

to delete extra row from datagrid is, just make property...

Canuseraddrows="false";