Pages

Thursday, February 10, 2011

A Database WPF Application

I started fresh on WPF and decided to build an app which required storage of some kind of database.To my amaze it was a bit different than my other applications on Windows Forms.

Although the ADO .Net connectivity part was almost same there was much changes in the XML part.

Let me brief you regarding my design a bit

I have used a List view which holds a Grid View which holds my data in turn.The Grid view is a part of view property of my List view.Lets see how

<listview margin="8,9,11,125" name="listView1" itemssource="{Binding}" minwidth="250" minheight="200" columnspan="2" rowspan="2">
<listview.view>
<gridview>
<gridviewcolumn header="ID" displaymemberbinding="{Binding Path=ID}"></gridviewcolumn>
<gridviewcolumn header="Company" displaymemberbinding="{Binding Path=Company}"></gridviewcolumn>
<gridviewcolumn header="Email" displaymemberbinding="{Binding Path=Email}"></gridviewcolumn>
<gridviewcolumn header="Website" displaymemberbinding="{Binding Path=Website}"></gridviewcolumn>
<gridviewcolumn header="Phone" displaymemberbinding="{Binding Path=Phone}"></gridviewcolumn>
<gridviewcolumn header="Address" displaymemberbinding="{Binding Path=Address}"></gridviewcolumn>
<gridviewcolumn header="Field of Development" displaymemberbinding="{Binding Path=Field}"></gridviewcolumn>
<gridviewcolumn header="Notes" displaymemberbinding="{Binding Path=Notes}"></gridviewcolumn>
</gridview>
</listview.view>
</listview>

Now if you see above ItemsSource="{Binding}" is the crux of this binding.It tells that the List view View data come from a binding source.What Happens if I add a control to my listview.I tried it got an error which I will discuss in my next post.

Mean while My code behind file has the following code

SqlConnection con = new SqlConnection(@"Data Source=PC\SQLEXPRESS AttachDbFilename=|DataDirectory|\JAMS.mdf;Integrated Security=True;User Instance=True");
con.Open();
try
{
SqlCommand comm = new SqlCommand("SELECT ID, Company, Email, Website, Phone, Address, Field, Notes FROM JAMS_Companies", con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(dt);
Console.Write("Blah");
Console.Write(dt.Rows[0][0].ToString());
listView1.DataContext = dt.DefaultView;
}
catch (Exception e)
{
Console.Write(e.ToString());
}

As usual dt is bound to the list view.

Now I am trying to add a button to this Window.I will get back as soon as I get Success

1 comment:

  1. Oops I was unable to add the buttons to lust view as I had given Item Source.I cannot also Button to window direct.

    *I am new to WPF kindly let me know if I am wrong anywhere.

    SO I have redefined my code and added Grid to it and then in that grid I have added Buttons and list view

    Here is my revised code

    <Window x:Class="Job_Application_Management.View"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="View" Height="500" Width="1000" Loaded="Window_Loaded">
    <Grid Height="400" Width="1000">
    <Grid.RowDefinitions>
    <RowDefinition Height="136*" />
    <RowDefinition Height="198*" />
    <RowDefinition Height="2*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="141*" />
    <ColumnDefinition Width="356*" />
    </Grid.ColumnDefinitions>
    <ListView Margin="8,9,22,125" Name="listView1" ItemsSource="{Binding}" MinWidth="500" MinHeight="200" Grid.ColumnSpan="2" Grid.RowSpan="2">
    <ListView.View>
    <GridView>
    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=ID}"></GridViewColumn>
    <GridViewColumn Header="Company" DisplayMemberBinding="{Binding Path=Company}"></GridViewColumn>
    <GridViewColumn Header="Email" DisplayMemberBinding="{Binding Path=Email}"></GridViewColumn>
    <GridViewColumn Header="Website" DisplayMemberBinding="{Binding Path=Website}"></GridViewColumn>
    <GridViewColumn Header="Phone" DisplayMemberBinding="{Binding Path=Phone}"></GridViewColumn>
    <GridViewColumn Header="Address" DisplayMemberBinding="{Binding Path=Address}"></GridViewColumn>
    <GridViewColumn Header="Field of Development" DisplayMemberBinding="{Binding Path=Field}"></GridViewColumn>
    <GridViewColumn Header="Notes" DisplayMemberBinding="{Binding Path=Notes}"></GridViewColumn>
    </GridView>
    </ListView.View>
    </ListView>
    <Button Height="26" Margin="112,0,0,41" Name="buttonAdd" VerticalAlignment="Bottom" Click="btnAdd_Click" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Width="86">Add</Button>
    <Button Height="26" Margin="286,0,355,41" Name="buttonUpdate" VerticalAlignment="Bottom" Click="btnUpdate_Click" Grid.Row="1" Grid.Column="1">Update</Button>
    <Button Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Right" Margin="0,0,195,44" Name="buttonDelete" VerticalAlignment="Bottom" Width="75" Click="buttonDelete_Click">Delete</Button>
    </Grid>
    </Window>

    ReplyDelete