Tag: C#

The best data structures to manipulate data manually = DataTable + DataRow + DataView

Here is a simple class that populates GridView control.
This is the best method for doing MANUAL data manipulations.
Code is in C# and VB.NET formats

C#
<%@ Import Namespace="System.Data" %>
<html>
<script language="C#" runat="server">

 string SortField;

 ICollection CreateDataSource() {
 DataTable dt = new DataTable();
 DataRow dr;

 dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
 dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
 dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
 dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
 dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

 for (int i = 0; i < 9; i++) {
 dr = dt.NewRow();

 dr[0] = 9-i;
 dr[1] = "Item " + i.ToString();
 dr[2] = DateTime.Now;
 dr[3] = (i % 2 != 0) ? true : false;
 dr[4] = 1.23 * (i+1);

 dt.Rows.Add(dr);
 }

 DataView dv = new DataView(dt);
 dv.Sort = SortField;
 return dv;
 }

 void Page_Load(Object sender, EventArgs e) {
 if (!IsPostBack) {
 if (SortField == "") {
 SortField = "IntegerValue";
 }    
 BindGrid();
 }    
 }

 void MyDataGrid_Sort(Object sender, DataGridSortCommandEventArgs e) {
 SortField = (string)e.SortExpression;
 BindGrid();
 }

 void BindGrid() {
 MyDataGrid.DataSource = CreateDataSource();
 MyDataGrid.DataBind();
 }

</script>

<body>

 <h3><font face="Verdana">Basic Sorting in DataGrid</font></h3>

 <form runat=server>

 <ASP:DataGrid id="MyDataGrid" runat="server"
 AllowSorting="true"
 OnSortCommand="MyDataGrid_Sort"
 BorderColor="black"
 BorderWidth="1"
 CellPadding="3"
 Font-Name="Verdana"
 Font-Size="8pt"
 HeaderStyle-BackColor="#ccccff"
 HeaderStyle-ForeColor="black"
 />

</form>

</body>
</html>

VB.NET
<%@ Import Namespace="System.Data" %>

<html>
<script language="VB" runat="server">

 Dim SortField As String

 Function CreateDataSource() As ICollection

 Dim dt As DataTable
 Dim dr As DataRow
 Dim i As Integer

 'create a DataTable
 dt = New DataTable
 dt.Columns.Add(New DataColumn("IntegerValue", GetType(Integer)))
 dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
 dt.Columns.Add(New DataColumn("DateTimeValue", GetType(DateTime)))
 dt.Columns.Add(New DataColumn("BoolValue", GetType(Boolean)))
 dt.Columns.Add(new DataColumn("CurrencyValue", GetType(Double)))

 'Make some rows and put some sample data in
 For i = 1 To 9
 dr = dt.NewRow()
 dr(0) = 9-i
 dr(1) = "Item " & i.ToString()
 dr(2) = DateTime.Now.ToShortTimeString
 If (i Mod 2 <> 0) Then
 dr(3) = True
 Else
 dr(3) = False
 End If
 dr(4) = 1.23 * (i + 1)
 'add the row to the datatable
 dt.Rows.Add(dr)
 Next

 'return a DataView to the DataTable
 Dim dv as DataView = New DataView(dt)
 dv.Sort = SortField
 CreateDataSource = dv
 End Function        

 Sub Page_Load(sender As Object, e As EventArgs)
 If Not IsPostBack Then
 If SortField = "" Then
 SortField = "IntegerValue"
 End If   
 BindGrid
 End If
 End Sub

 Sub MyDataGrid_Sort(sender As Object, e As DataGridSortCommandEventArgs)
 SortField = e.SortExpression
 BindGrid
 End Sub

 Sub BindGrid()
 MyDataGrid.DataSource = CreateDataSource()
 MyDataGrid.DataBind
 End Sub

</script>

<body>

 <h3><font face="Verdana">Basic Sorting in DataGrid</font></h3>

 <form runat=server>

 <ASP:DataGrid id="MyDataGrid" runat="server"
 AllowSorting="true"
 OnSortCommand="MyDataGrid_Sort"
 BorderColor="black"
 BorderWidth="1"
 CellPadding="3"
 Font-Name="Verdana"
 Font-Size="8pt"
 HeaderStyle-BackColor="#ccccff"
 HeaderStyle-ForeColor="black"
 />

</form>

</body>
</html>

Event handling (consuming) between forms

How to handle event:

objEditClassForm = new EditClass();
objEditClassForm.FormClosed += new FormClosedEventHandler(this.EditClassForm_Closed);

To explain in details:

objEditClassForm => is an object of the class EditClass (which inherits from the Form class)

So, in short, “objEditClassForm” is a simple windows form!
FormClosed => is a property of the class EditForm, this property is an “event” property

It is defined as follows:

public event FormClosedEventHandler FormClosed;

– This is code is defined in the System.Windows.Forms.Form class (built-in in Visual Studio)
– We can see that the property is declared as event
– We can also see that they type of the event is pre-defined (FormClosedEventHandler)

it is also built-in in Visual Studio in System.Windows.Forms.FormClosedEventHandler file, and is simply defined as:

public delegate void FormClosedEventHandler(object sender, FormClosedEventArgs e);

We can see that defining an event can be extremely simple!!!

+= sign => it is a necessary syntax when defining an event handler for the event

FormClosedEventHandler => see above

this.EditClassForm_Closed => is a function that will handle our event

In my example, it looks as follows:

private void EditClassForm_Closed(object sender, EventArgs e){
_load_all();
}

NOTES:

If you close (dispose of) the class that was assigned an event handler, it will not contain the event handler after that.

You will have to do it again!!!

Another example from MSDN is here:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

public class MyForm : Form
{
private TextBox box;
private Button button;
public MyForm() : base()
{
box = new TextBox();
box.BackColor = System.Drawing.Color.Cyan;
box.Size = new Size(100,100);
box.Location = new Point(50,50);
box.Text = “Hello”;
button = new Button();
button.Location = new Point(50,100);
button.Text = “Click Me”;

// To wire the event, create
// a delegate instance and add it to the Click event.
button.Click += new EventHandler(this.Button_Clicked);
Controls.Add(box);
Controls.Add(button);
}

// The event handler.
private void Button_Clicked(object sender, EventArgs e)
{
box.BackColor = System.Drawing.Color.Green;
}

// The STAThreadAttribute indicates that Windows Forms uses the
// single-threaded apartment model.
[STAThreadAttribute]
public static void Main(string[] args)
{
Application.Run(new MyForm());
}

}