Thursday, November 24, 2011

gridview stuff

   <div>
    <asp:GridView ID="GridView1" runat="server"
              AutoGenerateColumns="False"
              CellPadding="2" ForeColor="#333333"
              DataKeyNames="EMPID"
           OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="CheckAll">
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server"
              AutoPostBack="true"
              OnCheckedChanged="chkSelectAll_CheckedChanged"/>
                                       
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server"
              AutoPostBack="true"
              OnCheckedChanged="chkSelect_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="EMPID" HeaderText="ID"
                SortExpression="ID"/>
<asp:TemplateField HeaderText="EMPName" SortExpression="EMPName">
<ItemTemplate>
<asp:TextBox ID="txtEName" runat="server"
             Text='<%# Bind("EMPNAME") %>' ForeColor="Blue"
             BorderStyle="none" BorderWidth="0px"
             ReadOnly="true" >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Location" SortExpression
="Location">
<ItemTemplate>
<asp:TextBox ID="txtLocation" runat="server"
             Text='<%# Bind("ADDRESS") %>'
             ForeColor="Blue" BorderStyle="none"
             ReadOnly="true">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SALARY" SortExpression
="SALARY">
<ItemTemplate>
<asp:TextBox ID="txtSAL" runat="server"
             Text='<%# Bind("SALARY") %>'
             ForeColor="Blue" BorderStyle="none"
             ReadOnly="true">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
    </div>
-----------------------
.cs file
-------------------------
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    private string GetConnectionString()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["DEMO"].ConnectionString;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Show();

        }

    }
    private void Show()
    {
        SqlConnection con = new SqlConnection(GetConnectionString());
        con.Open();
        DataTable dt = new DataTable();

        SqlCommand command = new SqlCommand("SELECT * from [EMPLOYEE]", con);
        SqlDataAdapter ada = new SqlDataAdapter(command);
        ada.Fill(dt);
       GridView1. DataSource = dt;
       GridView1.DataBind();
        con.Close();

    }
    protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
    {


        CheckBox chkAll =

           (CheckBox)GridView1.HeaderRow.FindControl("chkSelectAll");

        if (chkAll.Checked == true)
        {

            foreach (GridViewRow gvRow in GridView1.Rows)
            {

                CheckBox chkSel =

                     (CheckBox)gvRow.FindControl("chkSelect");

                chkSel.Checked = true;

                TextBox txtEName = (TextBox)gvRow.FindControl("txtEName");

                TextBox txtAdd = (TextBox)gvRow.FindControl("txtAdd");
                TextBox txtSal = (TextBox)gvRow.FindControl("txtSal");


                //txtEName.ReadOnly = false;

                //txtAdd.ReadOnly = false;
                ////txtSal.ReadOnly = false;

                //txtEName.ForeColor = System.Drawing.Color.Black;

                //txtAdd.ForeColor = System.Drawing.Color.Black;
                //txtSal.ForeColor = System.Drawing.Color.Black;

            }

        }

        else
        {

            foreach (GridViewRow gvRow in GridView1.Rows)
            {

                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");

                chkSel.Checked = false;

                TextBox txtEName = (TextBox)gvRow.FindControl("txtEName");

                TextBox txtAdd = (TextBox)gvRow.FindControl("txtAdd");
                TextBox txtSal = (TextBox)gvRow.FindControl("txtSal");

                //txtEName.ReadOnly = true;

                //txtAdd.ReadOnly = true;
                //txtSal.ReadOnly = true;

                //txtEName.ForeColor = System.Drawing.Color.Blue;

                //txtAdd.ForeColor = System.Drawing.Color.Blue;
                //txtSal.ForeColor = System.Drawing.Color.Blue;
                //Show();
            }

        }
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox status = ((CheckBox)(row.Cells[0].Controls[1]));

            if (status.Checked == true)
            {

                row.BackColor = System.Drawing.Color.Red;

            }
            else if (status.Checked == false)
            {
                row.BackColor = System.Drawing.Color.White;
            }

        }


    }
    //protected void rowedit(object sender, EventArgs e)
    //{
       
    //}
   

    protected void chkSelect_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox status = ((CheckBox)(row.Cells[0].Controls[1]));

            if (status.Checked == true)
            {

                row.BackColor = System.Drawing.Color.Red;

            }
            else if (status.Checked == false)
            {
                row.BackColor = System.Drawing.Color.White;
            }

        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      // Checking if row type
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        // find the textbox control
       TextBox txtSAL = e.Row.FindControl("txtSAL") as TextBox;
        // read the value from the datasoure
        Double Salary= Convert.ToDouble(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Salary")));
  
        if (Salary > 15000.00)
        {
          // get the cell where that textbox contains
             DataControlFieldCell d = txtSAL.Parent as DataControlFieldCell;
  
          // to change the backcolor
          d.BackColor = System.Drawing.Color.Green;
  
          // to change the row color by setting
           
          //e.Row.BackColor = System.Drawing.Color.Blue;
  
          // to change the text color like this
         
            txtSAL.ForeColor = System.Drawing.Color.Chocolate;
         }
      }
     }

}





No comments:

Post a Comment