?
Solved

Using a comma-delimited string to populate a checkboxlist!

Posted on 2006-04-12
11
Medium Priority
?
1,228 Views
Last Modified: 2008-07-29
In my C# ASP.Net application, I have a datagrid with templates.  One of the items
in the template is a checkboxlist.  I want to do my SELECT from SQL Server so that
it returns one row per row in the datagrid, so I want to get the value and text values
for the checkboxlist into one column in the Select statement.

In the code-behind, probably in the datagrid ItemDataBound event, I want to take the
comma-delimited values coming from the server and create a datasource for the
checkboxlist.  Can I do that?  :-)

Data in SQL Server
---------------------
Id          Text
----        -----------
1           Urban
2           Surburban
3           Countryside
4           City

In my Select statement, one column will look like this:
--------------------------------------------------------------
1,Urban,2,Surburban,3,Countryside,4,City

In the code-behind, I want to make it look like a table again so I can bind it to the checkboxlist.

Thanks!
0
Comment
Question by:JeffDrummond
  • 6
  • 4
11 Comments
 
LVL 14

Accepted Solution

by:
existenz2 earned 1000 total points
ID: 16435457
public struct Data {
  public int Id;
  public int Text;
}

string data = "1,Urban,2,Surburban,3,Countryside,4,City";
string[] results = data.Split(',');
Data[] myData = new Data[(int)Math.Round(results.Length/2)];
int j = 0;
for(int i = 0; i < data.Length; i++) {
    Data d = new Data();
    d.Id = int.Parse(data[i]);
    d.Text = data[i+1];
  myData[j] = d;
j++;
 i++;
}

checkboxlist cbl = new checkboxlist();
cbl.DataSource = myData;
cbl.DataKeyField = "Id";
cbl.DataMember = "Text";
cbl.DataBind();

This should work.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 1000 total points
ID: 16438492
Actually, if your data is in row fashion, why are you going to the extra work of packing the string, and then unpacking?  What is the advantage there?

Bob
0
 

Author Comment

by:JeffDrummond
ID: 16438826
I want all of the data related to my one datagrid item to be in one row from the select statement.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 16438957
That doesn't make sense, if you are still displaying the data in rows.  Is there something else there that I can't see that explains why you want all the data on 1 row?

Bob
0
 

Author Comment

by:JeffDrummond
ID: 16439272
Hi Bob:

I have a datagrid with a templated column.  One datagrid item contains a photo, some text, and a series of checkboxes.  
The checkbox values are all related to one datagrid item, so I want them to be returned in one row from the database.

On Datagrid_ItemDataBound, I will "unpack" the checkbox items, create a datasource from them and bind to a checkboxlist.

That's my idea anyway.  If there is a better way to do this please please let me know!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16439304
Let me see if I understand what you are talking about.

Are you saying that the data comes from the source in rows, and you want to display that one row in columns instead?

Bob
0
 

Author Comment

by:JeffDrummond
ID: 16439560
Bob, how can I send you a private link to the page, so you'll see what I mean?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16439600
My email address is in my profile.

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16446898
How is your page laid out?  Where is the DataGrid?  

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16478627
:)
0
 

Author Comment

by:JeffDrummond
ID: 16533802
Hi, I managed to do what I wanted to do using a user-defined function in the database to
created the comma-delimited string.  In the code-behind, I created a datatable and populated
it with the string values to create datasource for a datalist in my datagrid. The checkboxlist wasn't
the control I wanted because I really wanted an IMAGE of a checked checkbox, not an actual checkbox.
It worked out great.

function
---------
ALTER   FUNCTION dbo._udf_GetHomeSettingsByHomeId(@HomeID int)
RETURNS VARCHAR(1000) AS

BEGIN
         DECLARE @SETTINGLIST VARCHAR(1000)

         SELECT       @SETTINGLIST = COALESCE(@SETTINGLIST + ',', '') + CAST(T2.SETTINGSID AS VARCHAR(2)) + ',' + T2.SETTING
         FROM       _HOMESETTINGS T1
                  LEFT OUTER JOIN  _SETTINGS T2 ON T1.SETTINGID = T2.SETTINGSID
      WHERE      T1.HOMEID = @HomeID
      AND             T1.TYPEID = 1

         RETURN @SETTINGLIST
END

code-behind
--------------
//populate datalist with home type values
if (e.Item.Cells[3].Text != "0")
{
      System.Web.UI.WebControls.DataList dataList = (System.Web.UI.WebControls.DataList)e.Item.FindControl("dlHomeTypes");
      string data = e.Item.Cells[3].Text;
      string[] results = data.Split(',');
      DataTable dt = new DataTable();
      DataColumn dc1 = new DataColumn("ID");
      dt.Columns.Add(dc1);
      DataColumn dc2 = new DataColumn("Text");
      dt.Columns.Add(dc2);

      for(int i = 0; i < results.Length; i++)
      {
            DataRow dr = dt.NewRow();
            dr[0] = results[i].ToString();
            dr[1] = results[i+1].ToString();
            dt.Rows.Add(dr);
            i++;
      }
      dataList.DataSource = dt;
      dataList.DataBind();
}
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question