Databind Array to DataGrid


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];
                         finalSetOfWords[i] = "NNN";
               return finalSetOfWords;

Any help would be much appreciated

Who is Participating?
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;

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:

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.