Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Visual Web Developer 2005 » Using TextBox, Button and other controls in DataGrid

Using TextBox, Button and other controls in DataGrid

In this article I am showing that how we can use controls like as Button, TextBox etc. in a DataGrid. How we will design these controls with DataGrid?

Author Rank :
Page Views : 13144
Downloads : 128
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
ControlinDataGrid.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Using control in DataGrid is a need in project. But the main task is that how we will design control with DataGrid. Here in this article I am showing that how we can use control like as Button, TextBox in DataGrid. First of all  design these button, textbox or other control. Let see this by a simple programme.

Here I am using a DataGrid to show some record. Here phone number, I am showing  in a TextBox, and I also used a edit button on click the value of corresponding TextBox will update.

The aspx code for this is: 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Control(Button , TextBox) In a DataGrid</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

      <asp:DataGrid ID="gridedit" runat="server" Width="440px" OnItemCommand="Grid_ItemCommand"

          DataKeyField="id" BorderStyle="Ridge" GridLines="None" BorderWidth="2px" BorderColor="White" BackColor="White"

          AllowSorting="True" PagerStyle-HorizontalAlign="Center"  HorizontalAlign="Left" OnPageIndexChanged="gridedit_PageIndexChanged"

          Height="267px" PageSize=5 AllowPaging="true" AutoGenerateColumns="false" SelectedItemStyle-Width="100px">

 

     <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>

     <HeaderStyle Font-Bold="True" ForeColor="#FFFFFF" BackColor="#A53A6A"></HeaderStyle>

     <FooterStyle BackColor="beige" />

     <PagerStyle Font-Bold="true" Mode=NumericPages Font-Underline="true"/>

 

        <Columns>

          <asp:BoundColumn DataField=id HeaderText="ID"  Visible="false">

          <ItemStyle BackColor="graytext" />

          <HeaderStyle BackColor="graytext" />

          </asp:BoundColumn>

                    <asp:BoundColumn DataField=name HeaderText="Name">

                           <ItemStyle BackColor=GhostWhite />

                    </asp:BoundColumn>    
               

                    <asp:BoundColumn DataField=F_name HeaderText="F_Name">

                          <ItemStyle BackColor=GhostWhite />

                    </asp:BoundColumn>

                    <asp:BoundColumn DataField=l_name HeaderText="L_Name">

                        <ItemStyle BackColor=GhostWhite />

                    </asp:BoundColumn> 
       

                   <asp:BoundColumn DataField=City HeaderText="City">

                        <ItemStyle BackColor=GhostWhite />

                  </asp:BoundColumn> 
       

                  <asp:BoundColumn DataField=State HeaderText="State">

                       <ItemStyle BackColor=GhostWhite />

                  </asp:BoundColumn>  

                 

                  <asp:TemplateColumn HeaderText="Phone"><ItemStyle BackColor=GhostWhite />

                    <ItemTemplate>

                      <asp:TextBox ID="txtPhone" runat="server" Width="81px" Text='<%#DataBinder.Eval(Container.DataItem,"Phoneno")%>'>

                      </asp:TextBox>

                    </ItemTemplate>

                  </asp:TemplateColumn>

 

                  <asp:TemplateColumn>

                    <ItemTemplate>

                      <asp:Button ID="btn_Phoneupdate" CommandName="Update" runat="server" Text="Edit" />

                    </ItemTemplate>

                  </asp:TemplateColumn>

         </Columns>

       </asp:DataGrid>  

    </div>

  </form>

</body>

</html>

The aspx.cs code is:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

 

public partial class _Default : System.Web.UI.Page

{

    SqlDataAdapter da;

    DataSet ds = new DataSet();

    SqlConnection con;

    SqlCommand cmd = new SqlCommand();

    string connect = "Data Source=MCN0100;Initial Catalog=RecordEdit; uid=sa; Pwd=";

   

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            Binddata();

        }

    } 

   

    //Here we Bind the data

    public void Binddata()

    {

        con = new SqlConnection(connect);

        cmd.CommandText = "select * from record order by id Asc";

        cmd.Connection = con;

        da = new SqlDataAdapter(cmd);

        da.Fill(ds);

        con.Open();

        cmd.ExecuteNonQuery();

        gridedit.DataSource = ds;

        gridedit.DataBind();

        con.Close();

    }      

 

     // For Paging   

    public void gridedit_PageIndexChanged(object source, DataGridPageChangedEventArgs e)

    {

        gridedit.CurrentPageIndex = e.NewPageIndex;

        Binddata();

    }   

 

    public void Grid_ItemCommand(object source, DataGridCommandEventArgs e)

    {

        if (e.CommandName == "Update")

        {            

                con = new SqlConnection(connect);

                cmd.CommandText = "Update record set Phoneno=@Phoneno  where id=@id";

                TextBox txt=(TextBox)e.Item.Cells[6].FindControl("txtPhone");

                cmd.Parameters.Add("@Phoneno", SqlDbType.NVarChar).Value =Int64.Parse(txt.Text);

                cmd.Parameters.Add("@id", SqlDbType.Int).Value = gridedit.DataKeys[e.Item.ItemIndex];

                cmd.Connection = con;

                cmd.Connection.Open();

                cmd.ExecuteNonQuery();

                cmd.Connection.Close();

                gridedit.EditItemIndex = -1;

                Binddata();

        }   

    }    

}

When user run the application then the window will become look like as:



Figure 1.

If I edit the phone number.



Figure 2.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Rahul Kumar Saxena
Rahul shows great interests in working with Microsoft technologies. He specializes in the implementation of DataBase & Graphics. His area of expertise includes: C#, ASP.NET,ADO.NET,Windows Forms & Web Services. He hails from background , Master's in Computer Application. With programming he loves photography, traveling and reading books.
(Talabpur*)
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
email by eliza On February 3, 2009
email me at madamellyza@yahoo.com
Reply | Email | Modify 
dropdown menu button by eliza On February 3, 2009
i have a problem in creating and making dropdown menu button in my web...I'm use Visual web devloper 2008 Express Edition .. i'm newbie in this software..i have difficuties in solving this problem..can u help me sir..
Reply | Email | Modify 
import music by deepika On March 23, 2009
how to import music files in Visual Web Developer 2005?
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.