Tuesday, July 13, 2010

Distinct Values from DataTable

Here is a very simple way to grab distinct values from a DataTable and throw them into another DataTable. "dt" is our original DataTable and "dt2" wants distinct person_id and person_name.

DataTable dt2 = dt.DefaultView.ToTable(true, new string[2] { "person_id", "person_name" });

Friday, October 16, 2009

Sort a DataTable

Here is a very easy way to sort the records in a DataTable by one of the columns.

    public static DataTable SortDataTable(DataTable dt, string sortColumn, string direction)
{
DataTable tempDataTable = dt.Clone();
int rowCount = dt.Rows.Count;
int columnCount = dt.Columns.Count;
DataRow[] allRows = dt.Select(null, sortColumn + " " + direction.ToUpper());

for (int i = 0; i < rowCount; i++)
{
object[] rowArray = new object[columnCount];
for (int j = 0; j < columnCount; j++)
{
rowArray[j] = allRows[i][j];
}
DataRow tempRow = tempDataTable.NewRow();
tempRow.ItemArray = rowArray;
tempDataTable.Rows.Add(tempRow);
}

dt.Rows.Clear();
for (int i = 0; i < tempDataTable.Rows.Count; i++)
{
object[] rowArray = new object[columnCount];
for (int j = 0; j < columnCount; j++)
{
rowArray[j] = tempDataTable.Rows[i][j];
}
DataRow tempRow = dt.NewRow();
tempRow.ItemArray = rowArray;
dt.Rows.Add(tempRow);
}

return (dt);
}

Friday, October 9, 2009

ResolveUrl

If you ever need to resolve the location of a resource in your code behind, use this sweet little function. This is especially useful in a Master Page or a User Control where the relative location of a resource will change.

Here is an example that dynamically adds some JavaScript onmouseout code for an image rollover:

imgImage.Attributes.Add("onmouseout", "src='" + ResolveUrl("~/Images/something_active.gif") + "'");

Friday, September 11, 2009

How to Add an Event Handler to Control Created in Code Behind

I hope that title makes sense to you. Basically I'm building a big form in the code behind. Sucks. But in my case I had to do it and also had to set AutoPostback and add an EventHandler to some checkboxes.

cb = new CheckBox();
cb.AutoPostBack = true;
cb.CheckedChanged += new System.EventHandler(this.CB_CheckedChanged);


"CB_CheckedChanged" is a method I created to handle the cb checks. You can basically use the same code for RadioButtonLists etc.

Thursday, September 10, 2009

Format Date in GridView

I always forget how to do this one. If you need to format a date a certain way in a
GridView, try this out:

<asp:BoundField DataField="the_date" HeaderText="Date" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="false" />

Friday, September 4, 2009

Syntax error: Missing operand after 's' operator.

Don't bother trying to filter a DataView by setting the RowFilter property to a string with a single quote. Make sure you escape the quotes first:

dv.RowFilter = "Something = 'O'Malley'";

becomes...

dv.RowFilter = "Something = 'O''Malley'";

Thursday, August 27, 2009

Oracle Client on Windows 7

Oracle sucks, but we're forced to use it at work. Of course Oracle can't get off their collective asses and put out a compatible client even after Windows 7 has, for all intents and purposes, been released. Here's a potential fix though:

http://msutic.blogspot.com/2009/08/how-to-instal-oracle-client-11g-on.html