• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Databind Array to DataGrid

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
BeginningWebDesign
Asked:
BeginningWebDesign
1 Solution
 
NipNFriar_TuckCommented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now