Hash

Entries tagged as ‘.NET’

Binding a List with DataGridView

March 31, 2009 · 2 Comments

DataGridView control for WinForms was introduced in .NET 2.0 and VS 2005. Its a pretty flexible control offering a lot more than DataGrid control in the previous version. Recently I was looking for a control which inherently support embedded control in its cells and DataGridView came up as a strong candidate. It has a native support of some basic embedded controls like TextBox, CheckBox etc. I explored it a little more and found it extremely easy and quick to program. My usage of this control is not much advanced which I describe here.

It has been built for displaying relational data so you can bind it to a DataTable from the database or an array of business objects. What I have done here is that I used a BindingList object which is also provided with .NET and is a useful variant of List object. The advantage of BindingList is that it can act as a sink of events while simple List object can’t.  It provides a PropertyChangedEventHandler which is called when a row is added/updated or deleted. So you simple bind this list to your DataGridView and both objects will remain synchronized. If you add/delete/update something to one the other object will simple pick it up from the event. Have a look at this:

private void MyForm_Load(object sender, EventArgs e)
{
             FillUpDataGridView();
             MyDataGridView.DataSource = Settings.MyBindingList;
}

The method FullUpDataGridView reads some business objects from a data source into the BindList object and the next statement displays the data in the control. You also need to bind DataGridView columns to properties of these business objects to specify which properties are displayed and which are not and their respective order.

Binding properties with columns of DataGridView control

This image shows name of the propery FeedEnabled which has been bound with the column "Enabled". My business object looks like:

public class FeedViewRow : INotifyPropertyChanged
{
    private string _FeedUri;
    private bool _AutoUpdate;
    private bool _FeedEnabled;         // Parameterless constructor for serialization
    public FeedViewRow() {  }
    public FeedViewRow(string Uri, bool Auto, bool Enabled)
    {
        _FeedUri = Uri;
        _AutoUpdate = Auto;
        _FeedEnabled = Enabled;
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public bool FeedEnabled
    {
        get { return _FeedEnabled; }
        set { _FeedEnabled = value; this.NotifyPropertyChanged("FeedEnabled"); }
    }
    public string FeedUri
    {
        get { return _FeedUri; }
        set { _FeedUri = value; this.NotifyPropertyChanged("FeedUri"); }
    }
    public bool AutoUpdate
    {
        get { return _AutoUpdate; }
        set { _AutoUpdate = value; this.NotifyPropertyChanged("AutoUpdate"); }
    }
    private void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

Keep a string property for TextBox and bool for CheckBox.  You object should implement the INotifyPropertyChanged interface.

After this you can simply [de]serialize the BindingList object. For serialization:

IsolatedStorageFile Storage = IsolatedStorageFile.GetUserStoreForAssembly();
IsolatedStorageFileStream File = new IsolatedStorageFileStream(StorageFileName,
FileMode.Create, FileAccess.Write, FileShare.None, Storage);
XmlSerializer Serializer = new XmlSerializer(typeof(BindingList));
Serializer.Serialize(File, this);
File.Close();
Storage.Close();

This will write out an XML of the whole list. Simple!

 

Share this post : digg it! Facebook it! live it! reddit! technorati! yahoo!

Categories: .NET
Tagged: , , , , , , , , , , , ,

C# Winforms: Using a progress bar with Web Browser Control

March 31, 2009 · 2 Comments

The .NET web browser provides a number of different events including some which tells when the browser started navigating to a web page and when that navigation completed.

These events are:

private System.Windows.Forms.WebBrowser ItemBrowser;

 

this.ItemBrowser.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.ItemBrowser_Navigating);
this.ItemBrowser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.ItemBrowser_DocumentCompleted);
this.ItemBrowser.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(this.ItemBrowser_Navigated);

 

This shows the functions are being called at proper events. Now let us come to the code in those functions. The goal is to show a marquee in the status bar when a page is clicked and stop that marquee when navigation is fully complete.

private void ItemBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    BrowserLabel.Text = "Loading ... ";
    BrowserProgressBar.MarqueeAnimationSpeed = 80;
    BrowserProgressBar.Style = ProgressBarStyle.Marquee;
}

 

I have created two items in the status strip. A Label named BrowserLabel and a progress bar item named BrowserProgressBar. The text property of BrowserLabel is set to “Loading … “ when a hyperlink is created. And to start the marquee we set the style of progress bar to Marquee and its speed to 80. Note that as you increase the AnimationSpeed it will slow down. It will be fastest at 1 and slowest at 100 (I have set 100 as MaxSpeed, you can change that property as well).

Now we need to stop this animation when browsing is complete:

void ItemBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    BrowserProgressBar.MarqueeAnimationSpeed = 0;
    BrowserProgressBar.Style = ProgressBarStyle.Blocks;

    BrowserLabel.Text = "Ready";
}

 

I set the style back to Block and AnimationSpeed to “0”. Style is a property and it can start/stop the animation according to its value so you don’t need to call a “start” function as required in the previous versions. Also the value of Label is reset here.

One function is still remaining:

void ItemBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    if (!ItemBrowser.IsBusy)
    {
        BrowserProgressBar.MarqueeAnimationSpeed = 0;
        BrowserProgressBar.Style = ProgressBarStyle.Blocks;

        BrowserLabel.Text = "Ready";
    }
}

 

This function is also called when navigation is completed. Although in most cases the DocumentCompleted function is sufficient but bookmark links do not call that function so Navigated function will do in those cases.

 

Share this post : digg it! Facebook it! live it! reddit! technorati! yahoo!

Categories: .NET · CodeProject
Tagged: , , , , , , , , , ,

Winforms .NET: Docking problem with ToolStrip and StatusStrip

February 6, 2009 · 2 Comments

Today I created a C# Winform with a ToolStrip and a SplitContainer beneath it. I wanted the SplitContainer to fill all of the form’s area but should start just under the ToolStrip, just like what you see in a normal Windows Form. To cover all area on the form and resize the SplitContainer with Form resize action I set the Dock property to FILL and the ToolStrip’s Dock property to TOP. The result was a SplitContainer spanning the whole Form and a ToolStrip on top of it. No! I tried a couple of different Dock value combinations with these controls but every time the result was one of them hiding the other one. And if you don’t set the Dock property it won’t resize itself with the Form.  After a little research on the internet and trying with different options I figured it out. The solution was not with the Docking values combination, they were right at the very first instance. It is the “Z” order of your controls which matter. Either:

  • Right click on SplitContainer and select “Bring to Front”
  • Or call BringToFront() in the code

The ToolStrip has to be at the back but since its Dock is set to TOP it will always be shown. And since SplitContainer is now brought at the Front it can’t be hidden by the ToolStrip.

 

Categories: .NET
Tagged: , , , , , , , , , , ,

Creating one-to-one relationship in NHibernate

November 18, 2008 · Leave a Comment

A one-to-one relationship between two tables can be created by:

  • Adding a column to the parent table with some unique ID of the child table
  • Without any extra column: just inserting the same PK value in both tables

NHibernate supports the second method (correct me) and you can create this relationship using the “foreign” type ID generator and the <one-to-one> tags in both mappings. Today I created the following mappings for two classes named Document and DocumentStatus.

<class name="Document" table="documents">
    <id name="Id" column="id">
          <generator class="foreign">
               <param name="property">Status</param>
          </generator>
    </id>
    <one-to-one name="Status" class="DocumentStatus" constrained="true"/>
    ... other properties ...
</class>

The trick here is just the foreign ID generator class. Now the mapping for DocumentStatus would be fairly simple.

<class name="DocumentStatus" table="documentstatus">
    <id name="Id" column="id">
       ... any generator class ...
    </id>
    <one-to-one name="Document" class="Document"/>
    ... other properties ...
</class>

The classes will have a property of the corresponding type to map this relationship so you can navigate both ways.

class Document {
... other stuff ...
DocumentStatus Status;
}
class DocumentStatus {
... other stuff ...
Document Document;
}

Whenver you insert a new object in Document, the corresponding DocumentStatus object will be created and its PK will be assigned to Document object as well.

Great Stuff!

Categories: .NET
Tagged: , , , , , , , ,

Persisting Custom Data Types with NHibernate

November 14, 2008 · Leave a Comment

NHibernate provides native support for primitive types/enumerations only. What if I want to save something like a custom data type, say for example a System.Uri object? NHibernate provides a fairly simple way to do that which is an interface actually called IUserType. It has got a whole bunch of methods which should be implemented by your class.

    public interface IUserType
    {
        SqlType[] SqlTypes { get; }
        System.Type ReturnedType { get; }
        bool Equals( object x, object y );
        object NullSafeGet( IDataReader rs, string[] names, object owner );
        void NullSafeSet( IDbCommand cmd, object value, int index );
        object DeepCopy( object value );
        bool IsMutable { get; }
    }

The following method of the interface tells the data base type:

public SqlType[] SqlTypes
{
get
{
// We store our Uri in a single column in the database that can contain a string
SqlType[] types = new SqlType[1];
types[0] = new SqlType(DbType.String);
return types;
}
}

And this method tells the .NET type to be built for the value.

public System.Type ReturnedType
{
get { return typeof(Uri); }
}

This is how you build the .NET datatype from the value:

public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
// We get the string from the database using the NullSafeGet used to get strings
string uriString = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
// And save it in the Uri object. This would be the place to make sure that your string
// is valid for use with the System.Uri class, but i will leave that to you
Uri result = new Uri(uriString);
return result;
}

This is how you translate the value back to database type.

public void NullSafeSet(IDbCommand cmd, object value, int index)
{
// Set the value using the NullSafeSet implementation for string from NHibernateUtil
if (value == null)
{
NHibernateUtil.String.NullSafeSet(cmd, null, index);
return;
}
value = value.ToString(); // ToString called on Uri instance
NHibernateUtil.String.NullSafeSet(cmd, value, index);
}

The rest of the method are simple, and you can find them on the internet as well (where I found them :-) )

Categories: .NET
Tagged: , , ,