Solved

Databind Array to DataGrid

Posted on 2004-10-31
276 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
Question by:BeginningWebDesign
    1 Comment
     
    LVL 7

    Accepted Solution

    by:
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now