Real World Scenario: DataGridView Link Click

Finally, I got time to blog another post on Real World Scenario. This time I received an email from a final year student. As mentioned earlier, the purpose of blogging about these issues is to share solution with others facing similar problem.

Scenario: (copied from email)

I’m facing some problems in my project. In one of the form I am using DataGridview to display my data… ! as mention in the attached JPEG. All columns are Databound. (except View Detail Column).
I want, when i click on ‘View’ all the data of particular row will display it new form im detail… ! Is it possible ??

The answer to this query is quite simple. DataGridView exposes an event ‘CellContentClick’ to which you can subscribe and handle the event. Consider the code snippet:

//subscribe the event first
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);

//handle the event
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 4) //4 is the index of Column with link (DataGridViewLinkColumn)
    {
        DetailForm detailForm = new DetailForm();
        detailForm.ShowDialog();
    }
}

and that will give you the desired functionality:) Thank you again for contacting me. If you have any issue related to .NET, send me an email and I will try my level best to solve the query for you.

Real World Scenario

Greetings!

From time to time, I receive emails from software developers around the world, specially from Middle East and Asia region, discussing some technical issue. So while I was answering an email today, I thought that it would be great if I create a category “Real World Scenario” on my blog and post the solution there so that many others could benefit.

But first please note some points 🙂

  • Being an MVP doesn’t mean that I know everything 🙂 but I will try my level best to help you in providing solution
  • The solution may not be the best approach to solve a particular problem or there may be a better way to code
  • Also, I may not post the exact question and code asked from person but will create question of similar situation otherwise I need to get permission from that person first

So this is my first post on the series of Real World Scenario.

Scenario: Binding a ComboBox to a Generic List of Custom class A with Display member as another class B exposed as public property in class A

Consider the following code snippet below:

public class B
{
    public string Info { get; set; }
    public int Id { get; set; }
}
public class A
{
    public string Name { get; set; }
    public B Information { get; set; }
}

We created two classes A and B and added some automatic properties in them. Most of the time, we want to bind our ComboBox (let’s say comboBox1) with List and Display member as some primitive data type, something like:

//populate some data
A obj1OfA= new A { Name = "Test1" , Information=new B { Info= "Info Test 1", Id = 1} };
A obj2OfA= new A { Name = "Test2" , Information=new B { Info= "Info Test 2", Id = 2} };
listOfA.Add(obj1OfA);
listOfA.Add(obj2OfA);
//and bind the comboBox1 with A
comboBox1.DataSource = listOfA;
comboBox1.DisplayMember = "Name";

In the code snippet above, we initialized a generic list of class A and then we created two objects of A using object intializer, a feature of C# 3.0, and finally added the objects in list along with binding of list to combobox.

So that will do our job because Name is a public property of type string in class A and it will display in Name easily. The question was that when you want to set Display Member as Information like:

comboBox1.DataSource = listOfA;
comboBox1.DisplayMember = "Information";

it will not display appropriate data in the combo box and that will be something like

WindowsFormsApplication1.B
WindowsFormsApplication1.B

and that is not what we wanted to display. The reason is that it actually calls ToString method and that returns type name in case when display member is Information. So the solution is that you need to override ToString() method in the class B and return the desired data that you want to display. So let’s go ahead and change our class B:

public class B
{
    public string Info { get; set; }
    public int Id { get; set; }
    public override string ToString()
    {
        return Info;
    }
}

and now when we execute our code we will get appropriate output showing the value of Info property in class B bind to our combobox 🙂 That’s all !

Thank you for the email sent by you, The Developers. Interaction with community really gives me an opportunity to learn from you people as well and a chance to serve the community. Thank you once again! Have a fantastic time!

WCF – Browsing svc file – HTTP Error 404.3

Recently,at office, our laptops were replaced by new Sony VAIO with Vista Business SP1. So today when I hosted WCF service in IIS 7.0. I got an error like

HTTP Error 404.3 – Not Found
Description: The page you are requesting cannot be served because of the Multipurpose Internet Mail Extensions (MIME) map policy that is configured on the Web server. The page you requested has a file name extension that is not recognized, and is not allowed.

Solution:

What is intuitive here is that .svc extension is not recognized i.e. Http handler of .svc is not defined 🙂 So the solution I found was that:

Start –> Run –> optionalfeatures

wndFeaturesWCFmarkedAnd you will notice that WCF HTTP Activation and WCF Non-HTTP Activation both are unchecked, so all you have to do is to check both these items and click ok. Windows will take few minutes to update and after that your WCF Service will run smoothly on Vista and IIS7.

I just faced that problem today, so I thought it will be great to share with other developers or perhaps it can be used as reference in future for myself as well.

WCF Binding Selection

Windows Communication Foundation(WCF) Services exposes endpoint(s) and each endpoint has information of ABC i.e. Address, Binding and Contract.

Binding defines the underlying protocol, message encoding and transport details which are essential for client and service to communicate with each other. WCF provides several pre-configured bindings having different protocols, encoding scheme etc. Further these bindings can be customized. For details, visit “Windows Communication Foundation Bindings” at MSDN.

The WCF ships with the following pre-configured bindings:

Binding

Description

basicHttpBinding

This interoperable binding is commonly used as a replacement for earlier Web services based on ASMX (Active Server Methods). It supports Hypertext Transfer Protocol (HTTP) and Hypertext Transfer Protocol over SSL (HTTPS) transport protocols as well as text and Message Transmission Optimization Mechanism (MTOM) encoding methods.

wsHttpBinding

This secure and interoperable binding uses SOAP over HTTP and supports reliability, transactions, and security over the Internet. It supports HTTP and HTTPS transport protocols as well as text and MTOM encoding methods.

wsDualHttpBinding

This interoperable binding is commonly used for duplex service contracts because it supports bidirectional communication.

webHttpBinding

This secure and interoperable binding sends information directly over HTTP or HTTPS without creating a SOAP envelope. It is an efficient choice when SOAP is not required by the client.

wsFederationHttpBinding

This secure and interoperable binding supports federated security. It supports HTTP and HTTPS transport protocols as well as text and MTOM encoding methods.

netTcpBinding

This secure binding is used to send binary-encoded SOAP messages from one WCF computer to another. It uses Transmission Control Protocol (TCP) and includes support for reliability, transactions, and security.

netNamedPipeBinding

This secure binding should be used on a single WCF computer. Binary-encoded SOAP messages are sent over named pipes.

netMsmqBinding

This queued binding is used to send binary-encoded SOAP messages over MSMQ. Communication should occur between two computers.

netPeerTcpBinding

This secure binding is used for peer-to-peer communication over TCP. Communication should occur between two or more computers.

msmqIntegrationBinding

This interoperable binding can be used for existing MSMQ applications that use COM and native C++ application programming interfaces (APIs).

basicHttpContextBinding

This binding provides support for HTTP cookies and enables SOAP headers to exchange context.

netTcpContextBinding

This secure binding enables SOAP headers to be used in the exchange of content.

wsHttpContextBinding

This secure and interoperable binding enables SOAP headers to exchange context while also supporting reliability, transactions, and security.

*Table Taken from MCTS 70-503 Windows Communication Foundation By Microsoft Press, Copyright GrandMasters and Microsoft Corporation.

Choosing a Binding for WCF Service may seem confusing, initially, for developers but here is a flow diagram that will help you in selecting binding depending upon your scenario.

clip_image001
Reference: Programming WCF Services by Juwal Lovy- Oreilly

Perhaps the most commonly used in the market are basicHttpBinding and wsHttpBinding which uses SOAP 1.1 and SOAP 1.2 respectively. A good comparison of both these is done by Kurt CLAEYS on his blog. I hope this post cleared the picture in your mind 🙂

Have a Good Day with Services!