0
\$\begingroup\$

I'm creating a WPF application with a DataGrid. I'd like some advice on the code I've written:

<DataGrid  Margin="0,10,10,10" LoadingRow="DataGrid_LoadingRow"    Name="user_view" EnableRowVirtualization="False" Grid.Column="2"    CanUserReorderColumns="True" CanUserResizeColumns="True"  CanUserResizeRows="True" CanUserSortColumns="True" 
    AlternatingRowBackground="Gainsboro" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="True" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True">
    <DataGrid.Columns>          
        <DataGridTextColumn  Header="Name" Width="*" Binding="{Binding name}" ></DataGridTextColumn>
        <DataGridTextColumn Header="Surname" Width="*" Binding="{Binding surname}"></DataGridTextColumn>
        <DataGridTextColumn Header="Address" Width="*" Binding="{Binding address}"></DataGridTextColumn>

 // and so on
 // and so on
 // and so on

...are all the cells that I bound my SQL View to.

Now, to fill the DataGrid:

public void LoadView()
{
    try
    {
        SQLiteCommand command = new SQLiteCommand(connection);
        command.CommandText = MyApk.Properties.Resources.user_datagrid; 

        DataSet DST = new DataSet();
        DataTable DT = new DataTable();
        SQLiteDataAdapter SDA = new SQLiteDataAdapter(command);
        SDA.Fill(DT);
        this.user_view.ItemsSource = DT.AsDataView();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

And the query to create the view is basically:

CREATE VIEW IF NOT EXISTS user_details AS SELECT name,surname,address //more stuff here

The specific points I'd like addressed are:

  1. Is this the right way to fill the DataGrid?
  2. Is it hard coded?
  3. How can I make it even better?
\$\endgroup\$
3
  • \$\begingroup\$ Does it work as wanted ? \$\endgroup\$
    – Heslacher
    Aug 18, 2015 at 8:12
  • \$\begingroup\$ Yes it does, but if its possible to "make" it more "flexible" as questions says. \$\endgroup\$
    – sharp
    Aug 18, 2015 at 8:16
  • \$\begingroup\$ I'm not posting full code because of the unnecessary data. The cells are almost the same, only Name and Binding changes. \$\endgroup\$
    – sharp
    Aug 18, 2015 at 8:55

1 Answer 1

3
\$\begingroup\$
public void LoadView()
{
    try
    {
        SQLiteCommand command = new SQLiteCommand(connection);
        command.CommandText = MyApk.Properties.Resources.user_datagrid; 

        DataSet DST = new DataSet();
        DataTable DT = new DataTable();
        SQLiteDataAdapter SDA = new SQLiteDataAdapter(command);
        SDA.Fill(DT);
        this.user_view.ItemsSource = DT.AsDataView();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}  
  • DataSet DST = new DataSet(); is never used and should be deleted.
  • both SQLiteCommand and SQLiteDataAdapter are implementing IDisposable so you should enclose them in a using statement to automatically dispose them after their usage. In addition the constructor of the SQLiteCommand is overloaded to take a commandtext too.

  • variables should be named using camelCase casing and shouldn't be named using abbreviations so SDA could simply be dataAdapter and DT will be dataTable.

Implementing the mentioned points will lead to

public void LoadView()
{
    try
    {

        using (SQLiteCommand command = new SQLiteCommand(MyApk.Properties.Resources.user_datagrid, connection))
        using (SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command))
        {            
            DataTable dataTable = new DataTable();
            dataAdapter.Fill(dataTable);
            this.user_view.ItemsSource = dataTable.AsDataView();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}  

But now this is tightly coupled to the user_view control and the query MyApk.Properties.Resources.user_datagrid which is suboptimal.

If we pass an ItemsControl object and a string to this method it would be more flexible like so

public void LoadView(ItemsControl control, string commandText)
{
    try
    {

        using (SQLiteCommand command = new SQLiteCommand(commandText, connection))
        using (SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command))
        {            
            DataTable dataTable = new DataTable();
            dataAdapter.Fill(dataTable);
            control.ItemsSource = dataTable.AsDataView();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}   

and can be called like

LoadView(this.user_view, MyApk.Properties.Resources.user_datagrid);
\$\endgroup\$
2
  • \$\begingroup\$ This is great! Thank you! And one questions...do you have any advice about XAML? Do I have to do again manual binding or? \$\endgroup\$
    – sharp
    Aug 18, 2015 at 8:50
  • \$\begingroup\$ No idea about XAML, sorry ( no I am not, I don't like GUI ). \$\endgroup\$
    – Heslacher
    Aug 18, 2015 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.