Showing posts with label Gridview. Show all posts
Showing posts with label Gridview. Show all posts

Thursday, 6 July 2017

// // Leave a Comment

Connection Strings in web.config configuration file

Today, I am going to explain database connection String in web.config configuration file using GridView.

First of all drag-drop GridView from Data at asp page


Set the connection string in web.config file


 Open the GridView Source write the database connection code

Final Output after Execution the Code




Read More

Thursday, 3 November 2016

// // Leave a Comment

How to set Gridview column width dynamically in Asp.Net C#

How to set column- width of gridview in asp.net .We can use several methods to achieve it just like source code (grid view property), code behind(C#) and CSS.


First Method:-
we can set the columns width by using ItemStyle-Width property just like below :-

<asp:GridView ID="GridView1" runat="server"AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name"ItemStyle-Width="50px" />
                <asp:BoundField DataField="Contactno"HeaderText="Contactno" ItemStyle-Width="50px" />
                <asp:BoundField DataField="Brithday"HeaderText="Brithday" ItemStyle-Width="50px" />
            </Columns>
        </asp:GridView>

Second Method:-
We can call the columns’s width from code behind just like below :-
For this code we need to need to call RowDataBound.
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            GridView1.Columns[0].ItemStyle.Width = 50;
            GridView1.Columns[1].ItemStyle.Width = 50;
            GridView1.Columns[2].ItemStyle.Width = 50;
        }

    }
Third method:-
We can set the column width using css but we call this call on only code behind because we can not directly call the css in column from source code and if we call on this gridview then it will be set whole gridview width not the columns. We can use this method like below:-
Source code:-

 <style type="text/css">
        .columnwidth
        {
            width50px;
        }
  </style>

Codebehind:-

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i > e.Row.Cells.Count; i++)
            {
                e.Row.Cells[i].CssClass = "columnwidth";
            }
        }
    }

Read More