Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL Gridview , +
hello,
 
i need to show a gird view in a page having data with comma in between them.
 
example of data in the table..
emp id.........emp Language
1.................English
2.................Russian
1.................Spanish
1.................American
2.................Chinise
 

i need to show the grid view like:
 
emp id........emp language
1................English,Spanish,American
2.................Russian,Chinise
 

please help ...
 
thanx in advance.
Posted 27-Nov-12 18:34pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Ishan,
 
Write your query with the COALESCE option. This will resolve your problem.
 
SELECT empid ,REPLACE(RTRIM((SELECT empLanguage + ' ' FROM TableName WHERE ( empid  = Results.empid  ) FOR XML PATH (''))),' ',', ') AS NameValues
FROM TableName Results
GROUP BY empid 
 
Apply below code to bind checkbox
 
for(int i=0;i<lstCheckBox.Items.Count;i++)
{ 
   for(int j=0;j<ds.Table[0].Rows.Count;j++)
   {
      if(lstCheckBox.Items[i].Value == ds.Table[0].Rows[j]["LanguageId"].ToString())
           lstCheckBox.Items[i].Selected = true;
   }
}
  Permalink  
v4
Comments
ishandeb at 28-Nov-12 0:44am
   
hello Mukhtar,
can u give me an example please..
Mohd. Mukhtar at 28-Nov-12 0:57am
   
Yeah sure please have a look above query and try to run.
ishandeb at 28-Nov-12 1:41am
   
can u help me out once more...
how to repopulate this languages names to checkbox list...
Mohd. Mukhtar at 28-Nov-12 1:45am
   
If you want to repopulate then you need to split the language with "," separator and after that you can bind checkbox.
ishandeb at 28-Nov-12 1:54am
   
but i need to bind the checkbox with both the value and text.
language names are only the text but the values are the language id.
can u give me an example please.
Mohd. Mukhtar at 28-Nov-12 2:03am
   
In this case you need to execute select command on language table.
 
SELECT languageId, languageName FROM tblLanguage;
 
Then bind the result with checkboxlist.
ishandeb at 28-Nov-12 2:07am
   
select T3.LanguageName ,T2.EmpName,T1.EmpId,T1.LanguageId
from EmpLanguage T1 left outer join
Employee T2 on T1.EmpId=T2.EmpId left outer join
Language T3 on T1.LanguageId=T3.LanguageId
where T1.EmpId=@EmpId
I have written this query but how to bind it in the check box list..
please help me.
Mohd. Mukhtar at 28-Nov-12 2:17am
   
Write below code in your aspx page.
 
<asp:CheckBoxList ID="lstCheckBox" runat="server" DataTextField='<%# Eval("LanguageName") %>' DataValueField=<%# Eval("LanguageId"%>'>


 
And Then bind the value with below code.
 
lstCheckBox.DataSource = queryResult; //query result should be dataset or datatable.
lstCheckBox.DataBind();
ishandeb at 28-Nov-12 2:24am
   
actually the checkboxlist is already binded i just need to check the boxes according to the language he has selected earlier.
Mohd. Mukhtar at 28-Nov-12 2:45am
   
Hmmm
Mohd. Mukhtar at 28-Nov-12 2:56am
   
Apply the below code.
 
for(int i=0; i
Mohd. Mukhtar at 28-Nov-12 3:06am
   
see updated answer.
ishandeb at 28-Nov-12 3:22am
   
string[] items = paramEmpLanguageInfo.LanguageName.Split(',');
for (int i = 0; i <= items.GetUpperBound(0); i++)
{

for (int j = 0; j < cklLanguage.Items.Count; j++)
{
ListItem currentCheckBox= cklLanguage.Items.FindByText(items[i].ToString())
if (currentCheckBox != null)
{
currentCheckBox.Selected = true;
}
}
}
can u solve this one out...
Mohd. Mukhtar at 28-Nov-12 3:36am
   
string[] items = paramEmpLanguageInfo.LanguageName.Split(','); for (int i = 0; i <= items.Length; i++)
{
ListItem currentCheckBox= cklLanguage.Items.FindByText(items[i].ToString())
if (currentCheckBox != null)
{ currentCheckBox.Selected = true; } }
ishandeb at 28-Nov-12 3:46am
   
no... the string items has the languages..
now i need to check the items in check box list with the items in the string items. if both of them are equal then the list in the check box will be selected..
for this one i have this code..
string[] items = paramEmpLanguageInfo.LanguageName.Split(',');
for (int i = 0; i <= items.GetUpperBound(0); i++)
{

for (int j = 0; j < cklLanguage.Items.Count; j++)
{
ListItem currentCheckBox= cklLanguage.Items.FindByText(items[i].ToString())
if (currentCheckBox != null)
{
currentCheckBox.Selected = true;
}
}
}
Mohd. Mukhtar at 28-Nov-12 4:43am
   
Yes correct. For this you are using FindByText, And this will fetch only those item which is avail into the array item, So it will just select that item.
ishandeb at 28-Nov-12 1:39am
   
Thanx a lot....
u made my day....
thank you very much...
Mohd. Mukhtar at 28-Nov-12 1:43am
   
Welcome :-)
Mohd. Mukhtar at 28-Nov-12 3:02am
   
Apply the below code.
 
for(int i=0; i
Member-515487 at 28-Nov-12 3:08am
   
what if i want to in place of empLanguage a number
it getting error Error converting data type varchar to numeric.
Mohd. Mukhtar at 28-Nov-12 3:21am
   
It is because empLanguage is char string and hence you can not convert it into integer.
ishandeb at 29-Nov-12 1:03am
   
hi how r u??
Mohd. Mukhtar at 29-Nov-12 1:17am
   
Hi,
 
me fine, What abt you?
 
BTW this is not a place for chatting ;-)
ishandeb at 30-Nov-12 3:41am
   
lol... well said...
can i have ur gmail id...
i can mail u if i need some help..
Mohd. Mukhtar at 30-Nov-12 3:49am
   
yeah sure,
 
mukhtar.bhumca@gmail.com

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 786
1 OriginalGriff 345
2 CPallini 160
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,705
1 Sergey Alexandrovich Kryukov 4,763
2 CPallini 4,570
3 George Jonsson 3,142
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 28 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100