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:
- Is this the right way to fill the
DataGrid
? - Is it hard coded?
- How can I make it even better?
1 Answer 1
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
andSQLiteDataAdapter
are implementingIDisposable
so you should enclose them in ausing
statement to automatically dispose them after their usage. In addition the constructor of theSQLiteCommand
is overloaded to take a commandtext too.variables should be named using
camelCase
casing and shouldn't be named using abbreviations soSDA
could simply bedataAdapter
andDT
will bedataTable
.
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);
-
\$\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– sharp2015年08月18日 08:50:17 +00:00Commented Aug 18, 2015 at 8:50
-
\$\begingroup\$ No idea about XAML, sorry ( no I am not, I don't like GUI ). \$\endgroup\$Heslacher– Heslacher2015年08月18日 08:54:23 +00:00Commented Aug 18, 2015 at 8:54
Name
andBinding
changes. \$\endgroup\$