Monday, April 26, 2010

How to bind Data in Customized Gridview

In asp.net gridview control is widely used control. It has lots of properties and attributes which help the developers for rapid development. Generally we can use Gridvew to show data in tabular format.
But It has a lots of events and features which is make this control a most powerful control for asp.net.
There are various types of field in Gridview such as:
1.BoundField
2.ImageField
3.HyperLinkField
4.ButtonField
5.TemplateField
6.CommandField
7.CheckBoxField

These fields gives developer more flexibility to customize the gridview. Paging is efficiently handling by Gridview it is one of cool feature of it. There are various way to bind data into gridview.

Today I want to focus how to bind data to a customized gridview. I have used this following way to bind customized gridview very frequently. Obviously,there may be more easier way to bind in customize gridview.
Here I have used BoundField,Templatefield to customize the grid.


Here is the aspx code for Gridview:

<asp:GridView ID="gvUser" runat="server" AutoGenerateColumns="False" OnRowCommand="gvUser_RowCommand"  ShowHeader="true">

<Columns>
                        <asp:boundfield datafield="User_ID" headertext="User ID" visible="false">
                        <asp:boundfield datafield="User_NAME" headertext="User Name"></asp:boundfield>
                        <asp:boundfield datafield="User_ADDRESS" headertext="User Address"></asp:boundfield>                       
                        <asp:templatefield headertext="Edit Info">
                            <itemtemplate>
                                <asp:linkbutton commandname="EditRowData" d="lbtnEdit" runat="server"></asp:linkbutton>
                            </itemtemplate>
                        </asp:templatefield>
                        <asp:templatefield headertext="Delete Info">
                            <itemtemplate>
                                <asp:linkbutton commandname="DeleteRowData" id="lbtnDelete" runat="server"></asp:linkbutton>
                            </itemtemplate>
                        </asp:templatefield>
                        <asp:templatefield headertext="User Status">
                            <itemtemplate>
                                <asp:linkbutton commandname="ActiveRowData" id="lbtnActiveRow" runat="server"></asp:linkbutton>
                            </itemtemplate>
                        </asp:templatefield>
                        
                    </asp:boundfield>
               
Here is the aspx.cs code:
I have placed this code under Search button click event:
 
protected void btnSearch_Click(object sender, EventArgs e)
    {  
      
        oUserDeatilList = GetList();//Get List Of data from database.
        //You can retrieve data as you like it may be in dataset or datatable no matter.
        //I have discussed after that scenario.


        DataTable oDataTable = new DataTable("Sample");
        oDataTable.Columns.Add(new DataColumn("User_ID", typeof(string))); ;
        oDataTable.Columns.Add(new DataColumn("User_NAME", typeof(string)));
        oDataTable.Columns.Add(new DataColumn("User_ADDRESS", typeof(string)));
   

        // list of User Info are loaded into dataTable
        if (oUserDeatilList != null)
        {
            if (oUserDeatilList.UserDetailList.Count > 0)
            {
                foreach (CUserDetail oUserDetail in oUserDeatilList.UserDetailList)
                {
                    DataRow row = oDataTable.NewRow();
                    row["User_ID"] = oUserDetail.UserID;
                    row["User_NAME"] = oUserDetail.PersonName;
                    row["User_ADDRESS"] = oUserDetail.UserAddress;
               
                    oDataTable.Rows.Add(row);
                }
            }
        }
        gvUser.Columns[0].Visible = true;
        gvUser.Columns[1].Visible = true;
        gvUser.Columns[2].Visible = true;
        gvUser.Columns[3].Visible = true;
        gvUser.Columns[4].Visible = true;
        //bind data to gridview
        gvUser.DataSource = oDataTable;
        gvUser.DataBind();
        gvUser.Columns[0].Visible = false;

        // to generat linkButton text
        for (int i = 0; i < gvUser.Rows.Count; i++)
        {
            LinkButton lblEdit = ((LinkButton)gvUser .Rows[i].FindControl("lbtnEdit"));
            lblEdit.Text = "Edit";//you can do whatever you like with this control's property

            LinkButton lblDelete = ((LinkButton)gvUser.Rows[i].FindControl("lbtnDelete"));
            lblDelete.Text ="Delete";//you can do whatever you like with this control's property

            LinkButton lblActive = ((LinkButton)gvUser.Rows[i].FindControl("lbtnActiveRow"));
            lblActive.Text = "Active";//you can do whatever you like with this control's property
        }
    }

  
   protected void gvUser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow gvRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
        string sButtonText = ((LinkButton)e.CommandSource).CommandName;
        string sUserID = gvRow.Cells[0].Text;
     

        if (sButtonText.Equals("EditRowData"))
        {
            if (sUserID.Length > 0)
            {
                LinkButton oLinkButton = (LinkButton)gvRow.Cells[5].FindControl("lbtnEdit");
                string sUrl = "AddEditUser.aspx?" + USERID + "=" + sUserID;
                Response.Redirect(sUrl, false);
                //do whatever you like
            }
        }
        else if (sButtonText.Equals("DeleteRowData"))
        {
            if (sUserID.Length > 0)
            {
                LinkButton oLinkButton = (LinkButton)gvRow.Cells[5].FindControl("lbtnDelete");
                //or do whatever you like
            }
        }
        else if (sButtonText.Equals("ActiveRowData"))
        {
            if (sUserID.Length > 0)
            {
                LinkButton oLinkButton = (LinkButton)gvRow.Cells[5].FindControl("lbtnActiveRow");
                if (oLinkButton.Text.Equals("ACTIVE")
                {
                    oLinkButton.Text = "INACTIVE";
                }
                else
                {
                     oLinkButton.Text = "ACTIVE";
                }
                //do whatever you like
             
            }
        }      
    }

Hope that it may help you.

5 comments:

usman said...

nice post. Helpful for developers.
Thanks

Unknown said...

welcome,pls provide me suggestion how can improve or optimize this or any better way.

jhons said...

yeah i like this.

dEvIlz said...

Nice post..

Unknown said...

welcom tahir

Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'

Exception: Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'...