Site Search

Monday, July 4, 2011

How to Add Gridview Header columns Vertically not Horizontally/How to add GridView Rows Vertically

This is scenario that many people stuck with so I did some investigation and finally came a good standard MSDN specified solution. So please take the advantage of it. Have nice cording ;)

Step 01
Add a gridview in to your aspx.(With no Columns)

 <asp:GridView ID="GridView" runat="server" BorderWidth="0px"                            Width="100%" ShowHeader="False">

Style it as you want.

Step 02
Code behind.Bind the data.

        private void PopulateGridView()
            GridView.DataSource = GetData();
        private DataTable GetData()

            DataTable table = new DataTable();
            table.Columns.Add(GridColumnName, typeof(string));
            table.Columns.Add(GridColumnEmail, typeof(string));
            table.Rows.Add("Asaj saii", "");

          GridFormatter gridformatter = new GridFormatter();
            DataTable datatable = gridformatter.FormatDataTable();

            gridformatter.AddDataRow(Localization.GetString("Name", this.LocalResourceFile), table.Rows[0][0].ToString(), datatable);
            gridformatter.AddDataRow(Localization.GetString("Email", this.LocalResourceFile), table.Rows[0][1].ToString(), datatable);

            return datatable;

public class GridFormatter


        public DataTable FormatDataTable()
            DataTable datatable = new DataTable();

            datatable.Columns.Add("Description", System.Type.GetType("System.String"));
            datatable.Columns.Add("Value", System.Type.GetType("System.String"));
            return datatable;
        public void AddDataRow(string description, string value, DataTable dt)
            DataRow datarow;
            datarow = dt.NewRow();
            datarow["Description"] = description;
            datarow["Value"] = value;


Step 03
Test it.Cheers!

Friday, July 1, 2011

How to add grid view columns by code behind/Add columns by programmatically/Add columns by server side

This code snippet show you how to add grid view columns by programmatically but nor actually by the design.
<asp:GridView ID="Grid" runat="server" Width="100%" AutoGenerateColumns="False" OnInit="Grid_Init">

<HeaderStyle CssClass="spn_gridheader" />
<RowStyle CssClass="spn_gridrow" />                                  
<PagerStyle  HorizontalAlign="Right" ForeColor="#009900" VerticalAlign="Middle" />


At grid view initialization,
protected void Grid_Init(object sender, EventArgs e)
            Grid.AutoGenerateColumns = false;
            Grid.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;

            BoundField date = CreateBoundField("Date","Header name","format"}");
            BoundField month = CreateBoundField("Month","Header name","format"}");
            BoundField year = CreateBoundField("Year","Header name","format"}");
              //Styling header columns
            date.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
            month.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
            year.ItemStyle.HorizontalAlign = HorizontalAlign.Right;


        private static BoundField CreateBoundField(string dataField, string header, string dataFormatString)
            BoundField field = new BoundField();
            field.DataField = dataField;
            field.HeaderText = header;
            if (!dataFormatString.Equals(""))
                field.DataFormatString = dataFormatString;
            field.SortExpression = dataField;
            field.HtmlEncode = false;
            return field;

You can add image column also if you want by using the Grid_RowDataBound event.
protected void GridViewRowDataBound(object sender, GridViewRowEventArgs e)

            if (e.Row.RowType == DataControlRowType.DataRow)
                ImageButton Button = new ImageButton();
                Button.ID = "actionNoteButton";

                    Button.ImageUrl = ResolveUrl("imagepath.jpg");
                    e.Row.Cells[column index].Controls.Add(Button);


Thursday, June 30, 2011

How to Pass value in to code behind when click on Gridview row - Server side

This will show how to pass key values in to code behind when you click on GridView row link button.

You have to use:
·         Command Name
·         OnRowCommand Event
·         TemplateField with Link button.
·         Add row command in to gridview
·         OnRowCommand="GridViewRowCommand"
·         Add template field with asp link button and set the command name as you wish.put the data table column name you want to bind in to the Text ad evalue.
                        <asp:LinkButton ID="Number" runat="server" Text='<%# Eval("Number") %>' CommandName="Number"
                            CommandArgument='<%# Eval("Number") %>'></asp:LinkButton>

·         At the row command event capture the value set by aspx and do your work..
protected void GridViewRowCommand(object sender, GridViewCommandEventArgs e)
            if (e.CommandName != null)
                if (e.CommandName == "Number")
                     string Number = e.CommandSource.ToString();
                     //Your code


How to add image link in to Gridview column

This will add cursor hand when hover on to the image.
                    <HeaderStyle HorizontalAlign="Left" Width="15px" />
                        <asp:HyperLink ID="Image" runat="server" />
protected void GridViewRowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                HyperLink rLink = (HyperLink)e.Row.Cells[column index].Controls[1];
                Link.ImageUrl = "image path";
                Link.NavigateUrl = "";

How to remove Gridview sort column Header underline - for beginners(C#)

code behind:
Use fallowing method in RowDatabound Event.
        private static void RemoveHeaderUnderLine(GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.Header)
                for (int cell = 0; cell < e.Row.Cells.Count; cell++)
                    if (e.Row.Cells[cell].Controls.Count > 0)
                        LinkButton Link = (LinkButton)e.Row.Cells[cell].Controls[0];
                        Link.Attributes.Add("style", "text-decoration:none;");

Creating a DataTable - for beginners(C#)

How to create a DataTable for bind Data

private static DataTable CreateDataTable()
            DataTable table = new DataTable();
            table.Columns.Add(AccountNumb, typeof(Int64));
            table.Columns.Add(Name, typeof(string));
            table.Columns.Add(Owner Address, typeof(string));

            table.Rows.Add("001225", "Gustavo Santaolalla", " frederick burner");
            table.Rows.Add("001220", "Gustavo Santaolalla", " frederick burner");
            return table;


Get started DNN - DNN for dummies

§ DDN Zip file(Required version ex:5.06.02)
§ DNN installation(5.6.2 Visual Studio starter kit)
§ MS SQL Express.
Standard way
 1.Creat a class library project.
 2.Install the Visual studio starter kit.
 3.Extract the DNN Zip file and copy it inside to the physical location of the created class library project is.
 4.Add the DNN location/Site as an existing web site to the project created early.
 5.Then run the site if success it will automatically add the DB in to SQL Server.
Way -2
 1.Change the configuration file by changing the Database.
 2.Then run the site it will install the site. with adding required tables in to DB
IIS configurations
 1.Create a virtual directory in IIS
 2.Give the Physical path to the DNN directory in the project.
 3.Giv "Network Services " Privileges to the DNN directory.