Hi,
I followed the suggestions on the web on how to make the Datagrid rows clickable. But when I debug the project, it hangs and I have to stop the debugger. Pls. help and thanks in advance.
codebehind:
public class chooseFacility : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblChooseFacility;
protected System.Web.UI.WebControls.TextBox txtChooseFacility;
protected System.Web.UI.WebControls.Table tblResults;
protected System.Web.UI.WebControls.Label lblSelect;
protected System.Web.UI.WebControls.Button btnChooseFacility;
protected System.Web.UI.WebControls.Label lblEnter;
protected System.Web.UI.WebControls.TextBox txtSearch;
protected System.Web.UI.WebControls.Button btnSearch;
protected System.Web.UI.WebControls.DataGrid dgResults;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
PopulateTable();
}
private void PopulateTable()
{
int i = 0;
((ArrayList)Cache["aUserSites"]).Sort(new CArrayListSort());
((ArrayList)((ArrayList)Cache["aRouteSites"])[0]).Sort(new CArrayListSort());
((ArrayList)((ArrayList)Cache["aRouteSites"])[1]).Sort(new CArrayListSort());
DataTable dataTable = new DataTable();
dataTable.Columns.Add("Name");
dataTable.Columns.Add("Prov_Num");
DataRow dataRow;
for (i=0; i<((ArrayList)((ArrayList)Cache["aRouteSites"])[0]).Count; i++)
{
if (((ArrayList)Cache["aUserSites"]).BinarySearch(((ArrayList)((ArrayList)Cache["aRouteSites"])[1])[i], new CArrayListSearch()) > -1)
{
// Create new row from DataTable.
dataRow = dataTable.NewRow();
dataRow["Name"] = ((string)((ArrayList)((ArrayList)Cache["aRouteSites"])[0])[i]);
dataRow["Prov_Num"] = ((string)((ArrayList)((ArrayList)Cache["aRouteSites"])[1])[i]);
dataTable.Rows.Add(dataRow);
}
}
DataView dv = new DataView(dataTable);
Cache["dv"] = dv;
dgResults.DataSource = dataTable;
dgResults.DataBind();
}
public ArrayList GetSitesFromRoute(string cWildcard)
{
string cSqlStmt = "";
ArrayList aRouteSites = new ArrayList();
aRouteSites.Add(new ArrayList());
aRouteSites.Add(new ArrayList());
Sql.SqlConfig oSqlCfg = Sql.loadSqlConfig("webconfigs", "route");
if (oSqlCfg == null)
return null;
string connectionString = "server=" + oSqlCfg.Server + ";" +
"user id=" + oSqlCfg.User + ";" +
"password=" + oSqlCfg.Pass + ";" +
"database=" + oSqlCfg.Database + ";" +
"pooling=false;" +
"allowzerodatetime=true";
MySqlConnection connection = new MySqlConnection(connectionString);
if (cWildcard.Length < 1)
cSqlStmt = "select * from webconfigs.route";
else
cSqlStmt = "select * from webconfigs.route where NAME like '%" + cWildcard + "%'";
MySqlCommand command = new MySqlCommand(cSqlStmt, connection);
connection.Open();
MySqlDataReader dr = command.ExecuteReader();
while(dr.Read())
{
((ArrayList)aRouteSites[0]).Add( dr.GetString(dr.GetOrdinal("NAME")).Trim() );
((ArrayList)aRouteSites[1]).Add( dr.GetString(dr.GetOrdinal("PROV_NUM")).Trim() );
}
dr.Close();
command.Dispose();
connection.Close();
return aRouteSites;
}
public TableCell AddCell(string strItem)
{
return AddCell(strItem, 0, "");
}
public TableCell AddCell(string strItem, Int16 iWidth)
{
return AddCell(strItem, iWidth, "");
}
public TableCell AddCell(string strItem, Int16 iWidth, string strClass)
{
TableCell cell = new TableCell();
cell.Text = strItem;
cell.Attributes["class"] = strClass;
if (iWidth > 0)
cell.Width = iWidth;
return cell;
}
public TableRow AddCellToRow(TableCell cell)
{
TableRow row = new TableRow();
row.Cells.Add(cell);
return row;
}
public void CreateaTemp()
{
ArrayList aTemp = new ArrayList();
aTemp.Add(new ArrayList());
aTemp.Add(new ArrayList());
}
#region
Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
if (Session["oUser"] == null || Session.IsNewSession)
Session["oUser"] = new User.User();
ViewState["numberDiv"] = 1;
Cache["aUserSites"] = ((User.User)(Session["oUser"])).Sites;
Cache["aRouteSites"] = GetSitesFromRoute("");
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnChooseFacility.Click += new System.EventHandler(this.btnChooseFacility_Click);
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
this.dgResults.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgResults_ItemCreated);
this.dgResults.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgResults_SortCommand);
this.dgResults.SelectedIndexChanged += new System.EventHandler(this.dgResults_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgResults_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
// Two way sorting LOGIC
if( (((int)(ViewState["numberDiv"]))%2) == 0 )
((DataView)Cache["dv"]).Sort = e.SortExpression + " " + "ASC";
else
((DataView)Cache["dv"]).Sort = e.SortExpression + " " + "DESC";
ViewState["numberDiv"] = ((int)ViewState["numberDiv"]) + 1;
dgResults.DataSource = ((DataView)Cache["dv"]);
dgResults.DataBind();
}
private void btnChooseFacility_Click(object sender, System.EventArgs e)
{
}
private void btnSearch_Click(object sender, System.EventArgs e)
{
Cache["aRouteSites"] = GetSitesFromRoute(txtSearch.Text.Trim());
PopulateTable();
}
private void Old_btnSearch_Click()
{
TableCell cell = new TableCell();
//header row
TableRow headerRow = new TableRow();
cell = AddCell("Name", 0, "header");
headerRow.Cells.Add(cell);
cell = AddCell("Site", 0, "header");
headerRow.Cells.Add(cell);
tblResults.Rows.Add(headerRow);
tblResults.CellSpacing = 0;
tblResults.CellPadding = 1;
Sql.SqlConfig oSqlCfg = Sql.loadSqlConfig("webconfigs", "route");
if (oSqlCfg == null)
return;
string connectionString = "server=" + oSqlCfg.Server + ";" +
"user id=" + oSqlCfg.User + ";" +
"password=" + oSqlCfg.Pass + ";" +
"database=" + oSqlCfg.Database + ";" +
"pooling=false;" +
"allowzerodatetime=true";
MySqlConnection connection = new MySqlConnection(connectionString);
Cache["cSqlStmt"] = "select * from webconfigs.route where NAME like% " + txtSearch.Text.Trim() + " or where PROV_NUM like% " + txtSearch.Text.Trim();
MySqlCommand command = new MySqlCommand(((string)Cache["cSqlStmt"]));
command.Connection = connection;
connection.Open();
MySqlDataReader dr = command.ExecuteReader();
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds,"route");
dgResults.DataBind();
while(dr.Read())
{
TableRow Row = new TableRow();
Row.Attributes["onmouseover"] = "rowOver();";
Row.Attributes["onmouseout"] = "rowOut();";
Row.Attributes["style"] = "cursor:hand;";
Row.Attributes["onclick"] = "selectSite('" + dr.GetString(dr.GetOrdinal("PROV_NUM")).Trim() + "'";
cell = AddCell(dr.GetString(dr.GetOrdinal("NAME")).Trim() , 0, "lst");
Row.Cells.Add(cell);
cell = AddCell(dr.GetString(dr.GetOrdinal("PROV_NUM")).Trim() , 0, "lst");
Row.Cells.Add(cell);
tblResults.Rows.Add(Row);
}
dr.Close();
command.Dispose();
connection.Close();
}
private void dgResults_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void dgResults_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
/*For Datagrig's with alternating colors
if(e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover",
"this.style.backgroundColor='beige';this.style.cursor='hand'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#99B3FF';");
e.Item.Attributes.Add("onclick", "javascript:__doPostBack" +
"('_ctl0$DataGrid1$_ctl" +
((Convert.ToInt32(e.Item.ItemIndex.ToString()))+2) +
"$_ctl0','')");
}
if(e.Item.ItemType == ListItemType.Item)
{
e.Item.Attributes.Add("onmouseover",
"this.style.backgroundColor='beige';this.style.cursor='pointer'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#D9E2FF';");
e.Item.Attributes.Add("onclick", "javascript:__doPostBack" +
"('_ctl0$DataGrid1$_ctl" +
((Convert.ToInt32(e.Item.ItemIndex.ToString()))+2) +
"$_ctl0','')");
}*/
//For a single color Datagrid
if(e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.Item)
{
e.Item.Attributes.Add("onmouseover",
"this.style.backgroundColor='beige';this.style.cursor='pointer'");
e.Item.Attributes.Add("onmouseout",
"this.style.backgroundColor='#99B3FF';");
e.Item.Attributes.Add("onclick", "javascript:__doPostBack" +
"('" + dgResults.UniqueID + "','')");
}
}
public class CArrayListSearch : IComparer
{
public int Compare(object x,object y)
{
return string.Compare( x.ToString(), y.ToString(), true );
}
}
public class CArrayListSort : IComparer
{
public int Compare(object x,object y)
{
return string.Compare( x.ToString(), y.ToString(), true );
}
}