Sunday, February 1, 2009

Insert Custom Advertisement Row in GridView

3 comments
Inserting advertisements between,say every tenth row populated by a gridview


The ASPX

<asp:GridView ID="GridView1" runat="server"  
AutoGenerateColumns="False"  
Height="171px" 
DataSourceID="sdsProducts"
Width="189px" 
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName"/>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdsProducts" runat="server" 
ConnectionString='<%$ ConnectionStrings:NorthwindConnectionString %>'
SelectCommand="SELECT * FROM Products">
</asp:SqlDataSource>

The ASPX.CS
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.DataItem != null)
{
int intCurrentId = e.Row.RowIndex;
if ((intCurrentId + 1) % 10 == 0)
{
// Add a row before this record
Table tblGrid = (Table)this.GridView1.Controls[0];
// Get the rowindex of the current row
int intIndex = tblGrid.Rows.GetRowIndex(e.Row);
// Create a new row to hold our subheading
GridViewRow gvrSubHeading = new GridViewRow(intIndex, intIndex, DataControlRowType.Separator, DataControlRowState.Normal);
// Create a table cell for the row
TableCell cellHeader = new TableCell();
// Set the colspan of the cell to the width of the table
cellHeader.ColumnSpan = this.GridView1.Columns.Count;
// Set the text
cellHeader.Text = "Your Advertisement goes here";
// Add the cell to the row
gvrSubHeading.Cells.Add(cellHeader);
// Add the row to the table
tblGrid.Controls.AddAt(intIndex + 1, gvrSubHeading);
}
}

}

Thats all.

P.S: The DataBase used is Northwind

Courtesy : Samu Zhang - MSFT of http://forums.asp.net
The original VB version posted by Samu Zhang can be found here
Read more...

Wednesday, October 22, 2008

Remove a HTML Element using Javascript

3 comments
Suppose the ID of the element is "objID"

The JavaScript method.(We are writing a custom function)



function removeElement(objID) {
var node = document.getElementById(objID);
node.parentNode.removeChild(node);
}



The JQuery method.



$("#objID").remove();



Its all about Keeping it Simple.
What do you say? :)
Read more...

Thursday, October 16, 2008

Copy DataRows from one DataTable to Another

0 comments
"This row already belongs to another table" - Annoying, to say the least!

The workaround, simple but elusive.



public DataTable CopyDataTable(DataTable dtSource, int iRowsNeeded)
{

if (dtSource.Rows.Count > iRowsNeeded)
{
// cloned to get the structure of source
DataTable dtDestination = dtSource.Clone();
for (int i = 0; i < iRowsNeeded; i++)
{
dtDestination.ImportRow(dtSource.Rows[i]);
}
return dtDestination;
}
else
return dtSource;
}



Happy Coding!
Read more...