You are here:

C#/Data binding an Array to a data grid

Advertisement


Question
QUESTION: Hello Hosnieh

I have a web service which querys a database I have created a web client for this service and I need the results to be output into a datagrid which would have 8 columns. I have created the datagrid on the form but need to populate the table rows.

The data obtained from the service comes in the following format ( 32, Bidding App, COD123, A bidding application, 03/01/2010 00:00:00, 22/01/2010 00:00:00, C#, 16)

I have splitted these values in an array and each of these values need to be under a different column in the grid

Heres my code thus far...

if(txtTitle.Text != String.Empty)
         {
         Service service = new Service();
         string result = service.GetProjName(txtTitle.Text).ToString();

         string[] resultArray = result.Split(',');
         foreach (string r in resultArray)
         {
         // code here to add array to data grid
         
         }

Any help would be much appreciated!

Regards
Akin

ANSWER: Hello,
You need to use datatable and import rows in to it and then bind the datatable to your datagrid
please note that instead of column name you may use the columnname you want to use in your datagrid

if(txtTitle.Text != String.Empty)
         {
         Service service = new Service();
         string result = service.GetProjName(txtTitle.Text).ToString();

DataTable dt1 = new DataTable();
         dt1.Columns.Add("columnname1");
         dt1.Columns.Add("columnname2");
         dt1.Columns.Add("columnname3");

         dt1.Columns.Add("columnname4");
         dt1.Columns.Add("columnname5");
         dt1.Columns.Add("columnname6");
         dt1.Columns.Add("columnname7");
         dt1.Columns.Add("columnname8");
DataRow dr = dt1.NewRow();
         string[] resultArray = result.Split(',');

         dr["columnname1"]=r[0];
         dr["columnname2"]=r[1];
         dr["columnname3"]=r[3];
         dr["columnname4"]=r[4];
         dr["columnname5"]=r[5];
         dr["columnname6"]=r[6];
         dr["columnname5"]=r[7];
         dr["columnname6"]=r[8];
 dt.ImportRow(dr);
datagrid1.DataSource = dt1;
datagrid1.DataBind();

so if you have more than one row in your array I mean you have two dimension array then you can use "for" for second dimension.
but if you have just one row of information then you don't need any for.

         
 


---------- FOLLOW-UP ----------

QUESTION: Yes at times more then one row will be returned by the service so how would this for be implemented

Regards
Akin

Answer
I consider that the delimiter for each row are "|" and the delimiter between data are "."

if(txtTitle.Text != String.Empty)
         {
         Service service = new Service();
         string result = service.GetProjName(txtTitle.Text).ToString();

DataTable dt1 = new DataTable();
         dt1.Columns.Add("columnname1");
         dt1.Columns.Add("columnname2");
         dt1.Columns.Add("columnname3");

         dt1.Columns.Add("columnname4");
         dt1.Columns.Add("columnname5");
         dt1.Columns.Add("columnname6");
         dt1.Columns.Add("columnname7");
         dt1.Columns.Add("columnname8");
DataRow dr = dt1.NewRow();
         string[] resultArray = result.Split('|');
int i;
for(i=0;i<resultArray.length;i++)
{
         string[] resultSubArray = resultArray[i].Split(',');

         dr["columnname1"]=resultSubArray[0];
         dr["columnname2"]=resultSubArray[1];
         dr["columnname3"]=resultSubArray[3];
         dr["columnname4"]=resultSubArray[4];
         dr["columnname5"]=resultSubArray[5];
         dr["columnname6"]=resultSubArray[6];
         dr["columnname7"]=resultSubArray[7];
         dr["columnname8"]=resultSubArray[8];
dt1.ImportRow(dr);
}
datagrid1.DataSource = dt1;
datagrid1.DataBind();

}

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Hosnieh [Sara]

Expertise

Questions which I can answers: 1- Programming (C#.net, Vb.net, javascript, HTML) more Web applications 2- CSS 3-Databases (Ms SQL server -all versions) 4-OS (workstation and servers (windows)) 5- MS office 6- General problems in computer software I cannot answer other kinds of questions which I did not mention here.

Experience

my CV: http://iran-americamarket.com/Contact.aspx I have more than 8 years Experience in IT field. my core skills are : • Asp.net Developer (Portal, Dynamic websites ,e-commercial websites) • Visual studio.net 2003,2005,2008 (VB.net , C#.net , AJAX.net) • O.O programming (n-tier Architecture) • Database (SQL server 2000-2005, Oracle9i, Ms Access) , TSQL, Stored procedures, Triggers • IIS 5-6 , SSL (Installation and configuration SSL) , Network(TCP/IP ,Active directory , DHCP, … ), Windows 2000-2003 Server Configuration and administration • UML (usecase diagram, class diagram) , Microsoft Project • Graphics Design( Photoshop , Flash ) • JavaScript , CSS, DHTML, XML • Hardware (physical installation, configuration , and driver installation) • Ms Office all versions (Ms word, Ms Access, Ms Excel, Ms Outlook , Ms FrontPage, and etc) • OS (Windows –Linux) and VMware • Network Monitoring (Solarwinds) • Computer Teacher, English Teacher • MATLAB (simulations and computation projects)

Organizations
-

Publications
Translation Experience English to Farsi • Use brain scan to predict when people will buy products . May 2008 (Published in AI Magazine) – Medicine , AI • Totally tubular motors _Tomorrow automation technology : March 2008 (Published in AI Magazine) – AI , Mechanic, Industry • Improving PLC compatibility and function flexibility: March 2008 (Published in AI Magazine) – AI , Robotics , Industry • Make packaging lines more flexible: January 2008 (Published in AI Magazine) – AI , Robotics , Industry • Personal CNC : December 2007 (Published in AI Magazine) – AI , Medical , Industry • Multipurpose optical tools Characterize as MEMS: December 2007 (Published in AI Magazine)- optical physics • Managing Innovation: November 2007 (Published in AI Magazine) – IT Management • Calculating the total value of ownership (TVO):October 2007 (Published in San-ate Hooshmand Magazine) -IT and Economics

Education/Credentials
I am currently student and doing my Master in Computer Network Engineering

Awards and Honors
- Poster about new methods of management in Organization( Entrepreneurship ) - 2007

Past/Present Clients
-

©2012 About.com, a part of The New York Times Company. All rights reserved.