0

I'm connecting to SQL Database and trying to fill DataGrid with received DataTable, but DataGrid is empty

In my ViewModel I have:

DataTable:

private DataTable _myData;
public DataTable MyData 
{  get => _myData;
   set=> UpdateValue(value, ref _myData);
}

Connecting Method:

var connStr = new StringBuilder();
            const string agentsQuery =
                "select CONCAT(users.last_name, ' ', users.first_name) as Agent from users where users.valid_id = 1 group by users.id";

            connStr.AppendFormat("server={0};user={1};database=otrs;password={2}",
                Connect.IP,
                Connect.User,
                Connect.Password);

            Connect.Connection = new MySqlConnection(connStr.ToString());
            Connect.Connection.Open();

           using (var sqlCom = new MySqlCommand(agentsQuery,  Connect.Connection))
                {
                    sqlCom.ExecuteNonQuery();
                    var dataAdapter = new MySqlDataAdapter(sqlCom);
                    dataAdapter.Fill(_myData);

                }

XAML Code:

<DataGrid ItemsSource="{Binding Path=MyData}"
          AutoGenerateColumns="True">
</DataGrid>

Connection is working. I've tried to load data to List and bind to ComboBox and everything is OK. Also, I've tried to bind List and ObservableCollection to DataGrid but it doesn't work anyway.

1

1 Answer 1

1
using (var sqlCom = new MySqlCommand(agentsQuery, Connect.Connection))
                {
                    sqlCom.ExecuteNonQuery();
                    var dt = new DataTable();
                    var dataAdapter = new MySqlDataAdapter(sqlCom);
                    dataAdapter.Fill(dt);
                    MyData = dt;

                }

That's worked!

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

Comments

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.