Datagridview rows

Fill datagridview rows with data returned from a web service

Page 1 of 1

2 Replies - 3130 Views - Last Post: 16 November 2009 - 05:53 AM Rate Topic: -----

#1 judyp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-November 09

Datagridview rows

Posted 16 November 2009 - 04:51 AM

using System;
using System.Web;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Advantage
{
	public partial class FormSentItems : Form
	{
		public FormSentItems()
		{
			InitializeComponent();
		}

		public class SentItems : System.Collections.CollectionBase
		{
			#region Nested Classes & Local Types
			/// <summary>
			///Structural view of each sent message
			/// </summary>
			public class SentItem
			{


				#region Variables
				int id;
				DateTime timestamp;
				string recipient;
				string short_message;
				#endregion

				#region Properties
				public int Id
				{
					get { return id; }
					set { id = value; }
				}
				public DateTime Timestamp
				{
					get { return timestamp; }
					set { timestamp = value; }
				}
				public string Recipient
				{
					get { return recipient; }
					set { recipient = value; }
				}
				public string Short_Message
				{
					get { return short_message; }
					set { short_message = value; }
				}
				#endregion

				#region Constructors
				/// <summary>
				/// program initialization
				/// </summary>
				/// <param name="id"></param>
				/// <param name="timestamp"></param>
				/// <param name="recipient"></param>
				/// <param name="short_message"></param>
				public void SentItems(int _id, DateTime _timestamp, string _recipient, string _short_message)
				{
					id = _id;
					timestamp = _timestamp;
					recipient = _recipient;
					short_message = _short_message;
				}

				/// <summary>
				///
				/// </summary>
				public void SentItems()
				{
				}
				#endregion


			}
			/// <summary>
			/// Total Items in the list
			/// </summary>
			public int Counting
			{
				get
				{
					return this.List.Count;
				}
			}

			/// <summary>
			/// Fetches a single item from the collection
			/// </summary>
			/// <param name="index">Offset of the SentItem</param>
			/// <returns>SentItem type | Null on error or mismatch</returns>
			public SentItem this[int index]
			{
				get
				{
					if (index >= this.List.Count)
						if (this.List[index] != null)
							return (SentItem)this.List[index];
						else
							return null;
					else
						return null;
				}
			}
			#endregion



			/// <summary>
			/// Adds a single sent item into the list
			/// </summary>
			/// <param name="Item">Sent item object</param>
			public bool Add(SentItem Item)
			{
				try
				{
					this.List.Add(Item);
					return true;
				}
				catch
				{
					return false;
				}
			}

			/// <summary>
			/// Returns a range of sent items
			/// </summary>
			/// <param name="Index">Where to start reading from</param>
			/// <param name="Length">Total messages to be read</param>
			/// <returns>Generic List | Null</returns>
			public System.Collections.Generic.List<SentItem> FetchRange(int Index, int Length)
			{
				//local, temporary list
				System.Collections.Generic.List<SentItem> Lst = new List<SentItem>();

				try
				{
					//populate list
					for (int a = Index; a <= Length - 1; a++)
						Lst.Add((SentItem)this.List[a]);

					//return processed list
					return Lst;
				}
				catch
				{
					//return null on all errors
					return null;
				}
				finally
				{
					//clean up
					Lst = null;
				}
			}

		}


		private void FormSentItems_Load(object sender, EventArgs e)
		{

			SentItems list = new SentItems();

			List<SentItems.SentItem> items = list.FetchRange(0, 5);

			//Bind grid to list
			dataGridView1.DataSource = items;

			//Add columns
			dataGridView1.AutoGenerateColumns = false;
			dataGridView1.Columns.Add("IdColumn", "Id");
			dataGridView1.Columns.Add("RecipientColumn", "Recipient");
			dataGridView1.Columns.Add("TimestampColumn", "Timestamp");
			dataGridView1.Columns.Add("Short_MessageColumn", "Short_Message");
			//Assign column names
			dataGridView1.Columns["IdColumn"].DataPropertyName = "Id";
			dataGridView1.Columns["RecipientColumn"].DataPropertyName = "Recipient";
			dataGridView1.Columns["TimestampColumn"].DataPropertyName = "Timestamp";
			dataGridView1.Columns["Short_MessageColumn"].DataPropertyName = "Short_Message";

			//add rows
			object[] row1 = { };
			object[] row2 = { };
			object[] row3 = { };
			dataGridView1.Rows.Add(row1);
			dataGridView1.Rows.Add(row2);
			dataGridView1.Rows.Add(row3);

			if(dataGridView1.RowCount > 0)
			{
				//create web request
				System.Net.WebClient Request = new System.Net.WebClient();

				//Set request properties
				Request.Headers["Content-Type"] = "text/xml; charset=utf-8";
				Request.Encoding = System.Text.UTF8Encoding.UTF8;

				try
				{
					//fetch user id
					string user_id = Request.DownloadString("http://www.advantage.co.za/api/security.asmx/UserId?username=ABCD&password=4567");
					user_id = user_id.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<string xmlns=\"http://www.smsadvantage.co.za/api/\">", "");
					user_id = user_id.Replace("</string>", "");

					//non zero indexed records to stop retrieving to
					int readstart = 1, readend = 100;

					//get sent message
					string getresponse = Request.DownloadString("http://www.advantage.co.za/api/message.asmx/Sent?user_id=" + user_id + "&date_from=" + DateTime.Now.AddHours(6).ToString("yyyy-MM-dd hh:mm:ss") + "&date_to=" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "&readstart=" + readstart + "&readend=" + readend + "&order=" + SortOrder.Ascending);
					getresponse = getresponse.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<boolean xmlns=\"http://www.advantage.co.za/api/\">", "");
					getresponse = getresponse.Replace("</boolean>", "");
					bool getres = Convert.ToBoolean(getresponse);

					if (getres)
						MessageBox.Show(this, " ", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
					else
						MessageBox.Show(this, "Message send failed.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
				}

				catch (Exception ex)
				{
					MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
				}
				
			}
			
		}
	}
}



Its illogical,I know.I've just begun with C# 3 months ago.

Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

This post has been edited by judyp: 16 November 2009 - 11:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Datagridview rows

#2 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Datagridview rows

Posted 16 November 2009 - 05:02 AM

First off, you might want to add code tages to your code. Second off, could you clarify what error you are having or what the question is. I didn't really understand what you were looking for.
Was This Post Helpful? 0
  • +
  • -

#3 judyp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-November 09

Re: Datagridview rows

Posted 16 November 2009 - 05:53 AM

View PostAdkins, on 16 Nov, 2009 - 04:02 AM, said:

First off, you might want to add code tages to your code. Second off, could you clarify what error you are having or what the question is. I didn't really understand what you were looking for.


Hi Adkins
I want to populate a datagridview's rows using a certain method from a web service.I'm not sure as to how I should fill the rows,how to go about it.Not sure if my code makes any sense logically.I only get something like "internal error 500".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1