Saturday, May 28, 2011

Gridview Row_DataBound Event

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Display the Product name in italics.
            e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
 
            // condtional formating
            string s1 = e.Row.Cells[1].Text.ToString();
            string s2 = "s";
            bool b = s1.Contains(s2);
            if (b)  // is true
            {
                e.Row.Cells[1].Text = "<u>" + e.Row.Cells[1].Text + "</u>";
            }
        }
        //********************************************
 
        if (e.Row.RowType == DataControlRowType.DataRow) 
        { 
            Button delbutton = (Button)e.Row.FindControl("btndelete"); 
             delbutton.Attributes.Add("onclick""javascript:return " + "confirm('Are you sure you want to delete this record :/n" +  
                                                                             DataBinder.Eval(e.Row.DataItem, "ProductName") + "')");
        }
 
        //*************************************
        //if (e.Row.DataItemIndex == -1)
        //    return;
 
        //e.Row.Attributes.Add("onMouseOver",   
        //      "this.style.cursor='hand';");
        //e.Row.Attributes.Add("onclick",
        //      this.GetPostBackClientEvent(GridView1,
        //      "Select$" + e.Row.RowIndex.ToString()));
 
        //**********************************
 
    }


2).

 
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
         int index = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "UnitPrice"));
            if (index <= 30)
            {
             // e.Row.ForeColor = System.Drawing.Color.Red; // whole row will be red
       e.Row.Cells[4].ForeColor = System.Drawing.Color.Red; // only the cell will be red.
            }
        }
        else
        {
 
        }
    }

3).........PAGER FORMATING

 protected void gridAllProjects_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            TableCell tc = new TableCell();
            TableCell tc1 = new TableCell();
 
            tc.Text = "[Page:";
            tc1.Text = "]";
            Table tbl = new Table();
            tbl = (Table)(e.Row.Cells[0].Controls[0]);
            tbl.Rows[0].Cells.AddAt(gridAllProjects.PageIndex, tc);
            tbl.Rows[0].Cells.AddAt(gridAllProjects.PageIndex + 2, tc1);
        }
    }

4). PreSelecting DropDownList

  <asp:GridView ID="grdData" DataKeyNames="userid,categoryname" Width="100%"  
runat="server">


 <EditItemTemplate>
  <asp:DropDownList ID="ddlCat" runat="server" Width="100px" 
     OnSelectedIndexChanged="ddlCat_SelectedIndexChanged"
     ToolTip='<%#Container.DataItemIndex %>'>
      </asp:DropDownList>
  </EditItemTemplate>

 
 protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList _ddlcategory = (DropDownList)e.Row.FindControl("ddlcategory");
            if (_ddlcategory != null)
            {
                _ddlcategory.DataSource = GetCategory();
                _ddlcategory.DataTextField = "categoryname";
                _ddlcategory.DataValueField = "categoryname";
                _ddlcategory.DataBind();
 
       _ddlcategory.SelectedItem.Text = 
                            grdData.DataKeys[e.Row.RowIndex].Values[1].ToString();

             }

        }    
   }






protected void gvCompanyInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Label l = (Label)(e.Row.FindControl("AddressType"));
            //l.ToolTip = e.Row.Cells[2].Text;
            if (e.Row.Cells[3].Text != "&nbsp;")
            e.Row.Cells[3].ToolTip = e.Row.Cells[3].Text;
            if (e.Row.Cells[5].Text != "&nbsp;")
            e.Row.Cells[5].ToolTip = e.Row.Cells[5].Text;
            if (e.Row.Cells[6].Text != "&nbsp;")
            e.Row.Cells[6].ToolTip = e.Row.Cells[6].Text;
            if (e.Row.Cells[4].Text.Length > 10)
            {
                e.Row.Cells[4].Text = e.Row.Cells[4].Text.Substring(0, 8) + "..";
            }
            if (e.Row.Cells[5].Text.Length > 15)
            {
                e.Row.Cells[5].Text = e.Row.Cells[5].Text.Substring(0, 12) + "..";
            }
            ImageButton btnDeleteGrid = (ImageButton)e.Row.FindControl("btnDelete");
            btnDeleteGrid.ToolTip = "Delete Address Information.";
 btnDeleteGrid.Attributes.Add("onclick", "return confirm('Are you sure you want to delete selected record(s)?');");

            e.Row.Cells[1].Text = (e.Row.DataItemIndex + 1).ToString();


        }
    }







0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More