Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Databind Array to DataGrid

Posted on 2004-10-31
1
Medium Priority
?
286 Views
Last Modified: 2010-04-15
Hi

Can anyone help with this problem, I want to databind the following variables to a datagrid called id="QueryResults"

Variables: HighlightBody, HighlightTitle, HighlightURL




The code for the array is below, anyone know how to do this.

void Page_Load(Object Src, EventArgs E)
{
inputString = Request.QueryString["TxtQuery"];
                         string[] words = GetQueryStringToArray(Request.QueryString["TxtQuery"].ToString()) ;
                    Word1 = words[0];
                    Word2 = words[1];
                    Word3 = words[2];
                    Word4 = words[3];
                    Word5 = words[4];
                    Word6 = words[5];
                    HighlightBody = spQueryResults.FieldValue("PageDescription", null);
                    HighlightBody = Regex.Replace(HighlightWord, Word1, "\b<strong>"+Word1+"</strong>", RegexOptions.IgnoreCase);
                    HighlightBody = Regex.Replace(HighlightWord, Word2, "\b<strong>"+Word2+"</strong>", RegexOptions.IgnoreCase);
                HighlightBody = Regex.Replace(HighlightWord, Word3, "\b<strong>"+Word3+"</strong>", RegexOptions.IgnoreCase);
                    HighlightBody = Regex.Replace(HighlightWord, Word4, "\b<strong>"+Word4+"</strong>", RegexOptions.IgnoreCase);
                HighlightBody = Regex.Replace(HighlightWord, Word5, "\b<strong>"+Word5+"</strong>", RegexOptions.IgnoreCase);
                    HighlightBody = Regex.Replace(HighlightWord, Word6, "\b<strong>"+Word6+"</strong>", RegexOptions.IgnoreCase);

                HighlightTitle = Regex.Replace(HighlightWord, Word1, "\b<strong>"+Word1+"</strong>", RegexOptions.IgnoreCase);
                    HighlightTitle = Regex.Replace(HighlightWord, Word2, "\b<strong>"+Word2+"</strong>", RegexOptions.IgnoreCase);
                HighlightTitle = Regex.Replace(HighlightWord, Word3, "\b<strong>"+Word3+"</strong>", RegexOptions.IgnoreCase);
                    HighlightTitle = Regex.Replace(HighlightWord, Word4, "\b<strong>"+Word4+"</strong>", RegexOptions.IgnoreCase);
                HighlightTitle = Regex.Replace(HighlightWord, Word5, "\b<strong>"+Word5+"</strong>", RegexOptions.IgnoreCase);
                    HighlightTitle = Regex.Replace(HighlightWord, Word6, "\b<strong>"+Word6+"</strong>", RegexOptions.IgnoreCase);
               
               
                    HighlightURL = Regex.Replace(HighlightWord, Word1, "<strong>"+Word1+"</strong>", RegexOptions.IgnoreCase);
                    HighlightURL = Regex.Replace(HighlightWord, Word2, "<strong>"+Word2+"</strong>", RegexOptions.IgnoreCase);
                HighlightURL = Regex.Replace(HighlightWord, Word3, "<strong>"+Word3+"</strong>", RegexOptions.IgnoreCase);
                    HighlightURL = Regex.Replace(HighlightWord, Word4, "<strong>"+Word4+"</strong>", RegexOptions.IgnoreCase);
                HighlightURL = Regex.Replace(HighlightWord, Word5, "<strong>"+Word5+"</strong>", RegexOptions.IgnoreCase);
                    HighlightURL = Regex.Replace(HighlightWord, Word6, "<strong>"+Word6+"</strong>", RegexOptions.IgnoreCase);
                   
               
          }
     public string[] GetQueryStringToArray(string inputString)
          {
            string[] finalSetOfWords = new string[6];
               char[] separators = {' '};
               string[] words = inputString.Split(separators);
               
               for ( int i=0; i < 6; i++)
               {
                    if(words.Length > i && inputString.Trim().Length > 0)
                    {
                         finalSetOfWords[i] = words[i];
                    }
                    else
                    {
                         finalSetOfWords[i] = "NNN";
                    }
               }
               return finalSetOfWords;
                 
          }

Any help would be much appreciated
Caz


0
Comment
Question by:BeginningWebDesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 2000 total points
ID: 12464083
Hi Caz, you can bind say an ArrayList of objects to a datagrid by

ArrayList al = new ArrayList();
DataGrid dg = new DataGrid();
dg.DataSource  = al;
dg.DataBind();

This works great especially if dealing with classes say for instance...

public class MyClass {
     private string body = string.Empty;
     private string title = string.Empty;
     private string url = string.Empty;
     public string Body {
          get { return "<strong>" + body + "</strong>"; }
          set { body = value; }
     }
     public string Title {
          get { return "<strong>" + title + "</strong>"; }
          set { title = value; }
     }
     public string URL {
          get { return "<strong>" + url + "</strong>"; }
          set { url = value; }
     }
     public MyClass() {}
     public MyClass( string Body, string Title, string URL ) {
          this.Body = Body;
          this.Title = Title;
          this.URL = URL;
     }
}

Then add MyClass to the al and you are done...

You may want to check out this site for more info:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclasstopic.asp

NOTE: The examples given here are done with a datatable, you can use a collection such as an ArrayList or classes instead of a datatable with the same result.
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

609 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