CASE in ORDER BY clause
Posted: April 22, 2008 Filed under: SQL Server 2005 Leave a commentGet columns count for GridView when using AutoGeneratedColumns is true
Posted: April 22, 2008 Filed under: ASP.NET 2 Leave a commentProtected
Sub GridView1_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound If e.Row.RowType = DataControlRowType.Header Thene.Row.BackColor = Drawing.Color.Yellow
For Each c As TableCell In e.Row.Cells If c.Text = "ID" Thenc.Text =
"ID change to 1…" End If If c.Text = "ID2" Thenc.Text =
"ID2 change to 2…" End If Next End If If e.Row.RowType = DataControlRowType.DataRow Then For i As Integer = 3 To e.Row.Controls.Count – 1e.Row.Cells(i).BackColor = Drawing.Color.Yellow
Next End If End SubOr
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
‘Same code as above
End Sub
C#:
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.BackColor = System.Drawing.
Color.Yellow;
foreach (TableCell c in e.Row.Cells)
{
if (c.Text == "ID")
{
c.Text =
"ID change to 1…";
}
if (c.Text == "ID2")
{
c.Text =
"ID2 change to 2…";
}
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 3; i <= e.Row.Controls.Count – 1; i++)
{
e.Row.Cells[i].BackColor = System.Drawing.
Color.Yellow;
}
}
}
Or
protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
//same code as above;
}
Remove some columns when export from GridView to Excel
Posted: April 9, 2008 Filed under: ASP.NET 2 Leave a commentIn your export routine, you can create an index list for the columns to be removed and use this list to turn these columns’ visible to false before you export them.
Here is what I use:
protected void btnExportExcel_Click(object sender, EventArgs e) { .... ArrayList ExcludedColumnsIndexList = new ArrayList(); ExcludedColumnsIndexList.Add("7");//the 8th column ExcludedColumnsIndexList.Add("8");//the 9th column ExportToExcel(filename, gridView1, ExcludedColumnsIndexList); }
public static void ExportToExcel(string myFile, GridView gv, ArrayList removeColumnslistIndex)
{
//Columns' index arraylist (removeColumnslistIndex) of GridView gv
if (removeColumnslistIndex!=null) { for (int j = 0; j < removeColumnslistIndex.Count; j++) { if (gv.HeaderRow != null) { gv.HeaderRow.Cells[Convert.ToInt32(removeColumnslistIndex[j])].Visible = false; } for (int i=0; i< gv.Rows.Count;i++) { gv.Rows[i].Cells[Convert.ToInt32(removeColumnslistIndex[j])].Visible = false; } if (gv.FooterRow != null) { gv.FooterRow.Cells[Convert.ToInt32(removeColumnslistIndex[j])].Visible = false; } } }
//other code omitted here
}
Don’t show item when the value is NULL in a DetailsView
Posted: April 9, 2008 Filed under: ASP.NET 2 Leave a commentprotected void DetailsView1_OnDataBound(object sender, EventArgs e) { if (DetailsView1.CurrentMode == DetailsViewMode.ReadOnly) { //DetailsViewMode.ReadOnly or DetailsViewMode.Edit System.Data.DataRowView rowView = (System.Data.DataRowView)DetailsView1.DataItem; //if (rowView.Row[2].ToString() == "") //use column index if (rowView.Row["nullableCol"].ToString() == "") //use column name { DetailsView1.Rows[2].Visible = false;//the column has null values } } }
VB.NET:
Protected Sub DetailsView1_OnDataBound(ByVal sender As Object, ByVal e As EventArgs) Handles DetailsView1.DataBound If DetailsView1.CurrentMode = DetailsViewMode.ReadOnly Then 'DetailsViewMode.ReadOnly or DetailsViewMode.Edit Dim rowView As System.Data.DataRowView = _ CType(DetailsView1.DataItem, System.Data.DataRowView) If rowView.Row(2).ToString() = "" Then DetailsView1.Rows(2).Visible = False End If End If End Sub
Export to excel through OLE automation
Posted: April 8, 2008 Filed under: SQL Server 2005 Leave a commentThere are a few options other than using OPENROWSET to export excel. From my research, the OLE Automation is a better option. Here is a good link to show you how you can use OLE automation for this task with a user Stored Procedure.
The link: http://www.simple-talk.com/sql/t-sql-programming/sql-server-excel-workbench/#first