Hide ImageButton if a column is Null

You can use an inline syntax to set  the ImageButton’s Visible property:

VB.NET: Visible=’<%# NOT String.IsNullOrEmpty(Eval("valColumn").ToString())%>

C#: Visible=’<%# ! String.IsNullOrEmpty(Eval("valColumn").ToString())%> 


Remove duplicate records from SQL Server 2005

WITH mycte AS

(

SELECT *, ROW_NUMBER() OVER (partition BY ID, colName

ORDER BY ID) AS num

FROM

tablename)

SELECT

* FROM mycte WHERE num > 1

–delete from mycte WHERE num > 1

You can delete the duplicates by uncommenting the above line.


Add Grouping Header in GridView

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

DataRowView drv = (DataRowView)e.Row.DataItem;

GridView _gv1 = (GridView)sender;

Table gvTable = (Table)e.Row.Parent;

 

if (drv["aDate"].ToString() != strGroup)

{

if (gvTable != null)

{

TableCell tc = new TableCell();

tc.Text = String.Format("{0:d}", drv["aDate"]);

tc.Height = Unit.Pixel(20);//you can change this to adjust to the space you want

tc.Attributes[

"Align"] = "Left";

tc.Font.Bold = true;

tc.Attributes[

"ColSpan"] = _gv1.Columns.Count.ToString(); ;

 

GridViewRow gr = new GridViewRow(0, 0, DataControlRowType.Separator, DataControlRowState.Normal);

gr.Cells.Add(tc);

gvTable.Controls.AddAt(_gv1.Controls[0].Controls.Count – 1, gr);

}

strGroup = drv[

"aDate"].ToString();

t += 1;

}

else

{

}

e.Row.Cells[0].Text =

"&nbsp;";

if (t % 2 == 0)

{

e.Row.BackColor = System.Drawing.

Color.Azure;

}

else

{

e.Row.BackColor = System.Drawing.

Color.GhostWhite;

}

 

}

}


Connect Access 2007 from code

protected void Page_Load(object sender, EventArgs e)

{

System.Data.

DataSet objDs = new System.Data.DataSet();

string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(".") + "\\App_Data\\Database1.accdb;" ;

System.Data.OleDb.

OleDbDataAdapter objDataAdapter = new System.Data.OleDb.OleDbDataAdapter("SELECT col1, col2 FROM [myTable1]", strConn);

objDataAdapter.Fill(objDs);

GridView1.DataSource = objDs.Tables[0].DefaultView;

GridView1.DataBind();

}


Display Excel 2007 data on GridView using ASP.NET 2.0

I came across this question from ASP.NET forum: http://forums.asp.net/t/1205825.aspx.

The connection string to use with Excel 2007 is the key for this question.

Answer:

Here is the code to show Excel 2007 spreedsheet on a GridView: 

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

Code:

VB.NET 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim objDs As New System.Data.DataSet()
       

Dim strConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _

"Data Source=" & Server.MapPath(".") & "\App_Data\Book1.xlsx;" & _

"Extended Properties=Excel 12.0"

  '//http://groups.google.com/group/microsoft.public.data.oledb/browse_thread/thread/791376f08015fc55 Dim objDataAdapter As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn) objDataAdapter.Fill(objDs) GridView1.DataSource = objDs.Tables(0).DefaultView GridView1.DataBind() End Sub

C#:
 
 protected void Page_Load(object sender, System.EventArgs e)
    {
        System.Data.DataSet objDs = new System.Data.DataSet();

       

string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="

+ Server.MapPath(".") + "\\App_Data\\Book1.xlsx;" +

"Extended Properties=Excel 12.0";
 
////http://groups.google.com/group/microsoft.public.data.oledb/browse_thread/thread/791376f08015fc55 

        System.Data.OleDb.OleDbDataAdapter objDataAdapter = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);

        objDataAdapter.Fill(objDs);

        GridView1.DataSource = objDs.Tables[0].DefaultView;
        GridView1.DataBind();
    }
// Here is another source to find the connection string: http://blogs.msdn.com/adamze/archive/2007/09/21/oledb-and-excel-2007.aspx 


How to change GridView generated table style: border-collapse:collapse;.

By default, GridView generates table style with  border-collapse:collapse;. It works for most cases but sometime we need to change the border style to  border-collapse:separate;.  There is no easy way to do through style settings.

Here is a code snippet to do this through GridView’s PreRender (or Oninit/OnLoad) event to change this style. 

OnPreRender="GridView1_OnPreRender"

protected void GridView1_OnPreRender(object sender, EventArgs e)
    {
        GridView1.CellSpacing = -1;      //remove the default value
        GridView1.Style["border-collapse"] = "seperate"; //add seperate to the style
    }
 

Add some space between GridView’s rows

There is  a way to add some space between GridView’s row through code:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

TableCell tc = new TableCell();

tc.Text =

"&nbsp;";

tc.Height =

Unit.Pixel(10);//you can change this to adjust to the space you want

GridView gv1=(GridView)sender;

tc.Attributes[

"ColSpan"] = gv1.Columns.Count.ToString(); ;

GridViewRow gr = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);

gr.Cells.Add(tc);

Table gvTable = (Table)e.Row.Parent;

gvTable.Controls.Add(gr);

}

}

This can be done through style:

Adjust the border bottom width property and assign the class through AlternatingRowStyle-CssClass and RowStyle-CssClass:

<

style type="text/css">

.bottomBorder

td {

border-color

:White;

border-style

: solid;

border-bottom-width: 100px

}

</style>

 

<asp:GridView ID="GridView1" runat="server"  AlternatingRowStyle-CssClass="bottomBorder" RowStyle-CssClass="bottomBorder"  ….. >