Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to find gridview control in visual studio?
Posted

Hello rbh007

What exactly you are referring to...
The Physical control or an event in grid view,

If you are refereeing to Physical Control then it is in the tool box -> Data -> Gridview
 
Share this answer
 
C#
GridView gridveiw1 = (GridView)  Page.FindControl("gridView");
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 18-May-12 7:35am    
Exact one, +5!
Matini 27-May-14 5:13am    
What about finding a gridview on a different .aspx page?
For example, I am filing a DataTable on Page1.aspx and I want to bind this DataTable onto a GridView on Page2.aspx.

I'm trying something like: GridView GV_ShopCart = (GridView)this.FindControl(lalala.Equals("GV_ShopCart"));

But that ain't working. In my case, the "lalala" is the .aspx page on which the gridview is located.
This Article will help you

Find Control in GridView[^]
 
Share this answer
 
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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;

public partial class adminMainPage : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=IPCNEW23\\SQLEXPRESS;Initial Catalog=rbh;Integrated Security=True");
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adap;
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }

    public void bind()
    {
        con.Open();
        ds.Clear();
        dt.Clear();
        string typevalue = "";
        string searchval = txtsearch.Text;

        if (lsttype.SelectedItem.Text == "<Select type>")
            typevalue = "%";
        else
            typevalue = lsttype.SelectedItem.Text;

        if (txtsearch.Text == "")
            searchval = "%";
        else
            searchval = "%" + txtsearch.Text + "%";

        adap = new SqlDataAdapter("select pID,pname,ptype,pdescr, CONVERT(varchar(10),startdate,110) as startdate,CONVERT(varchar(10),enddate,110) as enddate, pcost from projdetails where ptype like '"+typevalue+"' and ( pname like '"+searchval+"' or pdescr like'"+searchval+"' or pcost like '"+searchval +"') ",con);
        
        adap.Fill(ds, "x");
        dt = ds.Tables["x"];
        con.Close();

        projgrid.DataSource = dt;
        projgrid.DataBind();

        if (dt.Rows.Count == 0)
            lblnotif.Visible = true;
        else
            lblnotif.Visible = false;
        ViewState["orderpid"] = "asc";
        ViewState["orderpname"] = "asc";

        int tot = dt.Rows.Count;
        if (tot == 1)
        {
            foreach (GridViewRow e in projgrid.Rows)
            {
                Button ww = (Button)e.FindControl("cmdup");
                ww.Enabled = false;
                Button aa = (Button)e.FindControl("cmddown");
                aa.Enabled = false;
            }
        }
        else
        {
            foreach (GridViewRow e in projgrid.Rows)
            {
                if (e.RowIndex == 0)
                {
                    Button ww = (Button)e.FindControl("cmdup");
                    ww.Enabled = false;
                }
                else if (e.RowIndex == tot - 1)
                {
                    Button ww = (Button)e.FindControl("cmddown");
                    ww.Enabled = false;
                }
            }
        }
    }

    public void move(string mov,int rowid)
    {
        if (mov == "up")
        {
            int a = rowid - 1;
            int b = rowid;
            string temp;

            Label lpid1 = (Label)projgrid.Rows[a].FindControl("lblpid");
            Label lname1 = (Label)projgrid.Rows[a].FindControl("lblname");
            Label ltype1 = (Label)projgrid.Rows[a].FindControl("lbltype");
            Label ldesc1 = (Label)projgrid.Rows[a].FindControl("lbldesc");
            Label lsdate1 = (Label)projgrid.Rows[a].FindControl("lblenddate");
            Label ledate1 = (Label)projgrid.Rows[a].FindControl("lblstartdate");
            Label lcost1 = (Label)projgrid.Rows[a].FindControl("lblcost");

            Label lpid2 = (Label)projgrid.Rows[b].FindControl("lblpid");
            Label lname2 = (Label)projgrid.Rows[b].FindControl("lblname");
            Label ltype2 = (Label)projgrid.Rows[b].FindControl("lbltype");
            Label ldesc2 = (Label)projgrid.Rows[b].FindControl("lbldesc");
            Label lsdate2 = (Label)projgrid.Rows[b].FindControl("lblenddate");
            Label ledate2 = (Label)projgrid.Rows[b].FindControl("lblstartdate");
            Label lcost2 = (Label)projgrid.Rows[b].FindControl("lblcost");

            temp = lpid1.Text;
            lpid1.Text = lpid2.Text;
            lpid2.Text = temp;

            temp = lname1.Text;
            lname1.Text = lname2.Text;
            lname2.Text = temp;

            temp = ltype1.Text;
            ltype1.Text = ltype2.Text;
            ltype2.Text = temp;

            temp = ldesc1.Text;
            ldesc1.Text = ldesc2.Text;
            ldesc2.Text = temp;

            temp = lsdate1.Text;
            lsdate1.Text = lsdate2.Text;
            lsdate2.Text = temp;

            temp = ledate1.Text;
            ledate1.Text = ledate2.Text;
            ledate2.Text = temp;

            temp = lcost1.Text;
            lcost1.Text = lcost2.Text;
            lcost2.Text = temp;
            
        }

        if (mov == "down")
        {
            int a = rowid;
            int b = rowid + 1;
            string temp;

            Label lpid1 = (Label)projgrid.Rows[a].FindControl("lblpid");
            Label lname1 = (Label)projgrid.Rows[a].FindControl("lblname");
            Label ltype1 = (Label)projgrid.Rows[a].FindControl("lbltype");
            Label ldesc1 = (Label)projgrid.Rows[a].FindControl("lbldesc");
            Label lsdate1 = (Label)projgrid.Rows[a].FindControl("lblenddate");
            Label ledate1 = (Label)projgrid.Rows[a].FindControl("lblstartdate");
            Label lcost1 = (Label)projgrid.Rows[a].FindControl("lblcost");

            Label lpid2 = (Label)projgrid.Rows[b].FindControl("lblpid");
            Label lname2 = (Label)projgrid.Rows[b].FindControl("lblname");
            Label ltype2 = (Label)projgrid.Rows[b].FindControl("lbltype");
            Label ldesc2 = (Label)projgrid.Rows[b].FindControl("lbldesc");
            Label lsdate2 = (Label)projgrid.Rows[b].FindControl("lblenddate");
            Label ledate2 = (Label)projgrid.Rows[b].FindControl("lblstartdate");
            Label lcost2 = (Label)projgrid.Rows[b].FindControl("lblcost");

            temp = lpid1.Text;
            lpid1.Text = lpid2.Text;
            lpid2.Text = temp;

            temp = lname1.Text;
            lname1.Text = lname2.Text;
            lname2.Text = temp;

            temp = ltype1.Text;
            ltype1.Text = ltype2.Text;
            ltype2.Text = temp;

            temp = ldesc1.Text;
            ldesc1.Text = ldesc2.Text;
            ldesc2.Text = temp;

            temp = lsdate1.Text;
            lsdate1.Text = lsdate2.Text;
            lsdate2.Text = temp;

            temp = ledate1.Text;
            ledate1.Text = ledate2.Text;
            ledate2.Text = temp;

            temp = lcost1.Text;
            lcost1.Text = lcost2.Text;
            lcost2.Text = temp;
        }
    }

    protected void projgrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label id = (Label)projgrid.Rows[e.RowIndex].FindControl("lblpid");

        con.Open();
        cmd.Connection = con;
        cmd.CommandText="delete from projdetails where pid='"+ int.Parse(id.Text) +"' ";
        cmd.ExecuteNonQuery();
        con.Close();

        Response.Write("<script>alert('Row deleted')</script>");

        bind();
    }

    protected void projgrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        projgrid.EditIndex = e.NewEditIndex;
        bind();
    }
    
    protected void projgrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label id = (Label)projgrid.Rows[e.RowIndex].FindControl("lblpid2");
        TextBox tname = (TextBox)projgrid.Rows[e.RowIndex].FindControl("txtname");
        TextBox ttype = (TextBox)projgrid.Rows[e.RowIndex].FindControl("txttype");
        TextBox tdesc = (TextBox)projgrid.Rows[e.RowIndex].FindControl("txtdesc");
        TextBox tsdate = (TextBox)projgrid.Rows[e.RowIndex].FindControl("txtstartdate");
        TextBox tedate = (TextBox)projgrid.Rows[e.RowIndex].FindControl("txtenddate");
        TextBox tcost = (TextBox)projgrid.Rows[e.RowIndex].FindControl("txtcost");

        int prid = int.Parse(id.Text);

        con.Open();
        cmd.Connection = con;
        cmd.CommandText = "update projdetails set pname='" + tname.Text + "',ptype='" + ttype.Text + "',pdescr='" + tdesc.Text + "',startdate='" + tsdate.Text + "', enddate='" + tedate.Text + "',pcost='"+tcost.Text  +"' where pid='" + prid + "'  ";
        cmd.ExecuteNonQuery();
        con.Close();

        bind();
        Response.Write("<script>alert('Data updated.')</script>");
    }
    
    protected void projgrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        projgrid.EditIndex = -1;
        bind();
    }
    
    protected void cmdlogout_Click(object sender, EventArgs e)
    {
        Response.Redirect("login.aspx");
    }

    protected void projgrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "insert")
        {
            TextBox t1 = (TextBox)projgrid.HeaderRow.FindControl("tinsname");
            TextBox t2 = (TextBox)projgrid.HeaderRow.FindControl("tinstype");
            TextBox t3 = (TextBox)projgrid.HeaderRow.FindControl("tinsdesc");
            TextBox t4 = (TextBox)projgrid.HeaderRow.FindControl("tinssdate");
            TextBox t5 = (TextBox)projgrid.HeaderRow.FindControl("tinsedate");
            TextBox t6 = (TextBox)projgrid.HeaderRow.FindControl("tinscost");
                        
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "insert into projdetails values('" + t1.Text + "','" + t2.Text + "','" + t3.Text + "','" + t4.Text + "','" + t5.Text + "','" + t6.Text + "','user','password')";
            cmd.ExecuteNonQuery();
            con.Close();

            Response.Write("<script>alert('Data inserted')</script>");
            bind();
        }
        if (e.CommandName == "clear")
        {
            TextBox ta = (TextBox)projgrid.HeaderRow.FindControl("tinsname");
            ta.Text = "";
            TextBox tb = (TextBox)projgrid.HeaderRow.FindControl("tinstype");
            tb.Text = "";
            TextBox tc = (TextBox)projgrid.HeaderRow.FindControl("tinsdesc");
            tc.Text = "";
            TextBox td = (TextBox)projgrid.HeaderRow.FindControl("tinssdate");
            td.Text = "";
            TextBox te = (TextBox)projgrid.HeaderRow.FindControl("tinsedate");
            te.Text = "";
            TextBox tf = (TextBox)projgrid.HeaderRow.FindControl("tinscost");
            tf.Text = "";
        }

        if(e.CommandName=="pidsort")
        {
            con.Open();
            ds.Clear();
            dt.Clear();
            string typevalue = "";
            string searchval = txtsearch.Text;
            string s1 = ViewState["orderpid"].ToString();

            if (lsttype.SelectedItem.Text == "<Select type>")
                typevalue = "%";
            else
                typevalue = lsttype.SelectedItem.Text;

            if (txtsearch.Text == "")
                searchval = "%";
            else
                searchval = "%" + txtsearch.Text + "%";

            if (s1 == "asc")
            {
                adap = new SqlDataAdapter("select pID,pname,ptype,pdescr, CONVERT(varchar(10),startdate,110) as startdate,CONVERT(varchar(10),enddate,110) as enddate, pcost from projdetails where ptype like '" + typevalue + "' and ( pname like '" + searchval + "' or pdescr like'" + searchval + "' or pcost like '" + searchval + "') order by pid asc ", con);
                ViewState["orderpid"] = "desc";
            }
            else
            {
                adap = new SqlDataAdapter("select pID,pname,ptype,pdescr, CONVERT(varchar(10),startdate,110) as startdate,CONVERT(varchar(10),enddate,110) as enddate, pcost from projdetails where ptype like '" + typevalue + "' and ( pname like '" + searchval + "' or pdescr like'" + searchval + "' or pcost like '" + searchval + "') order by pid desc ", con);
                ViewState["orderpid"]="asc";
            }

            adap.Fill(ds, "x");
            dt = ds.Tables["x"];
            con.Close();

            projgrid.DataSource = dt;
            projgrid.DataBind();
            //--------------
            int tot = dt.Rows.Count;
            if (tot == 1)
            {
                foreach (GridViewRow ee in projgrid.Rows)
                {
                    Button ww = (Button)ee.FindControl("cmdup");
                    ww.Enabled = false;
                    Button aa = (Button)ee.FindControl("cmddown");
                    aa.Enabled = false;
                }
            }
            else
            {
                foreach (GridViewRow ee in projgrid.Rows)
                {
                    if (ee.RowIndex == 0)
                    {
                        Button ww = (Button)ee.FindControl("cmdup");
                        ww.Enabled = false;
                    }
                    else if (ee.RowIndex == tot - 1)
                    {
                        Button ww = (Button)ee.FindControl("cmddown");
                        ww.Enabled = false;

                    }
                }
            }
            //--------------
        }

        if (e.CommandName == "pnamesort")
        {
            con.Open();
            ds.Clear();
            dt.Clear();
            string typevalue = "";
            string searchval = txtsearch.Text;
            string s2 = ViewState["orderpname"].ToString();

            if (lsttype.SelectedItem.Text == "<Select type>")
                typevalue = "%";
            else
                typevalue = lsttype.SelectedItem.Text;

            if (txtsearch.Text == "")
                searchval = "%";
            else
                searchval = "%" + txtsearch.Text + "%";

            if (s2 == "asc")
            {
                adap = new SqlDataAdapter("select pID,pname,ptype,pdescr, CONVERT(varchar(10),startdate,110) as startdate,CONVERT(varchar(10),enddate,110) as enddate, pcost from projdetails where ptype like '" + typevalue + "' and ( pname like '" + searchval + "' or pdescr like'" + searchval + "' or pcost like '" + searchval + "') order by pname asc ", con);
                ViewState["orderpname"] = "desc";
            }
            else
            {
                adap = new SqlDataAdapter("select pID,pname,ptype,pdescr, CONVERT(varchar(10),startdate,110) as startdate,CONVERT(varchar(10),enddate,110) as enddate, pcost from projdetails where ptype like '" + typevalue + "' and ( pname like '" + searchval + "' or pdescr like'" + searchval + "' or pcost like '" + searchval + "') order by pname desc ", con);
                ViewState["orderpname"] = "asc";
            }

            adap.Fill(ds, "x");
            dt = ds.Tables["x"];
            con.Close();

            projgrid.DataSource = dt;
            projgrid.DataBind();

            int tot = dt.Rows.Count;
            if (tot == 1)
            {
                foreach (GridViewRow ee in projgrid.Rows)
                {
                    Button ww = (Button)ee.FindControl("cmdup");
                    ww.Enabled = false;
                    Button aa = (Button)ee.FindControl("cmddown");
                    aa.Enabled = false;
                }
            }
            else
            {
                foreach (GridViewRow ee in projgrid.Rows)
                {
                    if (ee.RowIndex == 0)
                    {
                        Button ww = (Button)ee.FindControl("cmdup");
                        ww.Enabled = false;
                    }
                    else if (ee.RowIndex == tot - 1)
                    {
                        Button ww = (Button)ee.FindControl("cmddown");
                        ww.Enabled = false;

                    }
                }
            }
        }

        if (e.CommandName == "up")
        {
            Button btn = (Button)e.CommandSource;
            GridViewRow gvr = (GridViewRow)btn.NamingContainer;
            int rowid = gvr.RowIndex; 

            move("up",rowid);
        }

        if (e.CommandName == "down")
        {
            Button btn = (Button)e.CommandSource;
            GridViewRow gvr = (GridViewRow)btn.NamingContainer;
            int rowid = gvr.RowIndex;

            move("down", rowid);
        }
    }
    
    protected void projgrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        projgrid.PageIndex = e.NewPageIndex;
        bind();
    }
   
    protected void lsttype_SelectedIndexChanged(object sender, EventArgs e)
    {
        bind();
    }
    protected void cmdsearch_Click(object sender, EventArgs e)
    {
        bind();
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900