<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="C#" runat="server">
void SubmitBtn_Click(Object sender, EventArgs e)
{
// Set the connection info to the database
string strDBPath = Server.MapPath(".") + "\\" + "population2.mdb";
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDBPath;
// Set select query string to return 4 columns of data
string sTable = "states";
string strAccessSelect = "SELECT year, [" + listBox1.SelectedItem.Text + "],
Increase, [% Increase] FROM " + sTable;
// Open connection to the database
OleDbConnection myConnection = new OleDbConnection(strAccessConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter(strAccessSelect, myConnection);
// Populate the dataset with the results of the select query
DataSet ds = new DataSet();
myCommand.Fill(ds, sTable);
// Update column names to include "(000s)"
DataColumnCollection drc = ds.Tables[sTable].Columns;
DataColumn dc = new DataColumn();
dc = drc[listBox1.SelectedItem.Text];
dc.ColumnName = "Pop (000s)";
dc = drc["Increase"];
dc.ColumnName = "Incr (000s)";
int iPrevPop = 0;
int iCurrPop = 0;
int iDiff = 0;
double Pct = 0.0;
int iCurrRow = 0;
DataRow prevRow = null;
DataColumn prevCol = null;
// Iterate through the dataset row by row
DataTable myTable = ds.Tables["states"];
foreach (DataRow myRow in myTable.Rows)
{
int iCurrCol = 0;
foreach (DataColumn myColumn in myTable.Columns)
{
// Column 2 value is year to year difference value
if (iCurrCol == 2)
{
if (iCurrRow == 0)
{
iPrevPop = 0;
}
else
{
// Replace null value in dataset with population difference value
iPrevPop = int.Parse(prevRow[prevCol].ToString());
iCurrPop = int.Parse(myRow[prevCol].ToString());
myRow[myColumn] = iCurrPop - iPrevPop;
}
}
// Column 3 value is year to year % difference value
if (iCurrCol == 3)
{
if (iCurrRow == 0)
{
iPrevPop = 0;
}
else
{
// Replace null value in dataset with % population difference value
iPrevPop = int.Parse(prevRow[prevCol].ToString());
iCurrPop = int.Parse(myRow[prevCol].ToString());
iDiff = iCurrPop - iPrevPop;
Pct = (double)iDiff / (double)iPrevPop;
int iTmp = (int)(Pct * 10000);
Pct = ((double)iTmp) / 100.0;
myRow[myColumn] = Pct;
}
}
if (iCurrCol == 1) prevCol = myColumn;
iCurrCol++;
}
iCurrRow++;
prevRow = myRow;
}
// Bind datagrid with dataset values
MyDataGrid.DataSource = ds.Tables[sTable].DefaultView;
MyDataGrid.DataBind();
}
</script>
...HTML tags...
<asp:DataGrid id="MyDataGrid"
Width="360"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="10pt"
GridLines="Horizontal"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
runat="server">
<HeaderStyle BackColor="#00aaaa" Font-Bold="true">
</HeaderStyle>
</asp:DataGrid>
...HTML tags...
|