Display two column values in a combo box [Resolved]

Posted by Akiii under C# on 10/10/2011 | Points: 10 | Views : 20587 | Status : [Member] | Replies : 7
Hi All,
Can anyone help me in this:-

How to display two columns in dropdownlist of combo box ?

Thanks and Regards
Akiii




Responses

Posted by: Blessyjees on: 10/10/2011 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi,

By using sql query you can combine two columns as single column and bind that column to combo box
For eg: firstname and last name

select firstname +' ' +lastname as name from tblename

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 10/11/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi Akiii,

in string.format -20 will create a space of 20 and remaining space will be filled with blank values
You can see all the last names are aligned. Please see the attached sample.
I have filled them with (_) to identify.

What I mean no need to create a new formatted column in SQL query then populate the new value in your collection etc , just add a property so that it will return the formatted value.
 Download source file

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 10/10/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,
Thank you @blessyjees and @parthiv for ur help.....
i did exactly like the above to bind the data in the combo box...

Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 10/10/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Akiii,

In case you do not want to change your existing store procedure or sql query, You could just add a property which will return the formatted property.
Lets say you have a class called Student
public class Student

{
public string FName{ get;set;}
public string LName {get;set;}

public string FullName
{
get { return string.Format("{0,-20}|{1}",FName,LName);
}
}

Now you can use FullName for binding.



Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Niladri.biswas on: 10/10/2011 [Member] Platinum | Points: 25

Up
0
Down
You can even use expression of the Add method of DataColumn.The overloaded method is
 public DataColumn Add(string columnName, Type type, string expression);



e.g.
DataTable dt = new DataTable();

dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Col1", typeof(string));
dt.Columns.Add("Col2", typeof(string));
dt.Columns.Add("ConcatenatedField", typeof(string), "Col1 + ' : ' +Col2");

Enumerable.Range(1, 10).ToList().ForEach(i => dt.Rows.Add(i, string.Concat("Col1", i), string.Concat("Col2", i)));

comboBox1.DataSource = dt;
comboBox1.DisplayMember = "ConcatenatedField";


Best Regards,
Niladri Biswas

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 10/11/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi....

@narayan
Thanks for answering but i didnt quite understood your code...!
Could you please explain it to me... ?
why there is -20 there ?

@Niladri......
thanks for replying...
Could you explain the code below :
 Enumerable.Range(1, 10).ToList().ForEach(i => dt.Rows.Add(i, string.Concat("Col1", i), string.Concat("Col2", i)));


Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 10/12/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi narayan....
Excellent app made to prove ur point....
As always you have been of immense help narayan...

Thanks and Regards
Akiii

Akiii, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response