Showing posts with label asp.net gridview. Show all posts
Showing posts with label asp.net gridview. Show all posts

Get checkbox control in gridview C# - findcontrol

get gridview checkbox asp.net c#
In this post we are going to see how to find checked checkboxes in gridview using c#. To find the checkboxes in gridview we have to iterate through the rows of gridview using gridviews rows collection. And then we can use findcontrol method of the row to get checkbox control. We need to pass Id of the checkbox to FindControl method.

Example to get checked checkboxes in gridview[C#]


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px"
CellPadding="3" BorderStyle="None" Font-Names="Arial">
<FooterStyle></FooterStyle>
<PagerStyle HorizontalAlign="Left"></PagerStyle>
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxSelect" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Item No." DataField="ItemNo">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Item Name" DataField="ItemName">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
</Columns>
</asp:GridView>

</div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</form>
</body>
</html>

In code behind on click of button we will iterate through gridviews rows and find out checked checkboxes.

using System.Data;

namespace aspnetcontrol
{
    public partial class GridviewImages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e) 
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = BindImageData();
                GridView1.DataBind();
            }
        }

       public DataTable BindImageData()     
        {
           
            DataTable dt = new DataTable();
           
            dt.Columns.Add(new DataColumn("ItemNo", typeof(int)));
            dt.Columns.Add(new DataColumn("ItemName", typeof(string)));
            // Create the four records
            DataRow dr = dt.NewRow();
            dr["ItemNo"] = 1;
            dr["ItemName"] = "Tea";
           
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ItemNo"] = 2;
            dr["ItemName"] = "Coffie";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            return dt;
        }

       protected void Button1_Click(object sender, EventArgs e)
       {
           // Iterate through the Products.Rows property
           foreach (GridViewRow row in GridView1.Rows)
           {
               CheckBox cb = (CheckBox)row.FindControl("CheckBoxSelect");

               if (cb != null && cb.Checked)
               {
                   //Do some stuff
                   
               }
           }
       }
    }

asp.net add checkbox in gridview

add checkbox in gridview

In asp.net we need to add checkbox in gridview to select the row in gridview. To add checkbox in gridview we have to add ItemTemplate inside the asp:TemplateField. And inside ItemTemplate we can add checkbox control. We will how to add checkbox in gridview in asp.net using C#.

Example to add checkbox in gridview


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px"
CellPadding="3" BorderStyle="None" Font-Names="Arial">
<FooterStyle></FooterStyle>
<PagerStyle HorizontalAlign="Left"></PagerStyle>
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxPurchase" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Item No." DataField="ItemNo">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Item Name" DataField="ItemName">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

Below code just binds the data to gridview.

using System.Data;

namespace aspnetcontrol
{
    public partial class GridviewImages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e) 
        {
            GridView1.DataSource = BindImageData();
            GridView1.DataBind();
        }

       public DataTable BindImageData()     
        {
           
            DataTable dt = new DataTable();
           
            dt.Columns.Add(new DataColumn("ItemNo", typeof(int)));
            dt.Columns.Add(new DataColumn("ItemName", typeof(string)));
            // Create the four records
            DataRow dr = dt.NewRow();
            dr["ItemNo"] = 1;
            dr["ItemName"] = "Tea";
           
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ItemNo"] = 2;
            dr["ItemName"] = "Coffie";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            return dt;
        }
    }
}

Images in gridview control - c# example

asp.net image in gridview
In asp.net we need to show images in gridview. To show image in gridview we have to use TemplateColumn in gridview and Image web control inside it. asp.net 2.0 have ImageField that we can use to view image in gridview.

Sample code to show image in gridview [asp.net c#]

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" Runat="server" AutoGenerateColumns="False" 
  BorderWidth="1px" CellPadding="3" BorderStyle="None" 
  Font-Names="Arial">
    <FooterStyle></FooterStyle>
    <PagerStyle HorizontalAlign="Left"></PagerStyle>
    <HeaderStyle Font-Bold="True"></HeaderStyle>
    <Columns>
        <asp:BoundField HeaderText="Image No." DataField="ImageID">
            <ItemStyle HorizontalAlign="Center" 
              VerticalAlign="Middle"></ItemStyle>
        </asp:BoundField>
        <asp:ImageField DataImageUrlField="ImagePath"></asp:ImageField>
    </Columns>
   </asp:GridView>
    </div>
    </form>
</body>
</html>

Below is C# code to show image in gridview

using System;
using System.Data;

namespace aspnetcontrol
{
    public partial class GridviewImages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e) 
        {
            GridView1.DataSource = BindImageData();
            GridView1.DataBind();
        }

       public DataTable BindImageData()     
        {
           
            DataTable dt = new DataTable();
           
            dt.Columns.Add(new DataColumn("ImageID", typeof(int)));
            dt.Columns.Add(new DataColumn("ImagePath", typeof(string)));
            // Create the four records
            DataRow dr = dt.NewRow();
            dr["ImageID"] = 1;
            dr["ImagePath"] = ResolveUrl("~/Images/image1.jpg");
           
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["ImageID"] = 2;
            dr["ImagePath"] = ResolveUrl("~/Images/image2.jpg");
            dt.Rows.Add(dr);
            dr = dt.NewRow();
       
            return dt;
        }
    }
}

In above code we have used TemplateCloumn of gridview and ImageField control inside it. Here we can set any datasource that contains image and bind it to gridview. Here in this example I have created sample datatable with image column and bind it to gridview to show image in gridview.

Get GridView SelectedIndexChanged event asp.net

gridview selectedindexchanged event
In this post we will see how to use SelectedIndexChanged Event of GridView in ASP.Net. SelectedIndexChanged event get fired when we click select button of the gridview row. We can use this event to perform some operation on form when row is selected like call custom routines, change labels, change titles..etc.

Sample code to see how SelectedIndexChanged event in gridview works [ASP.Net, C#]:


<asp:GridView ID ="GridView1" runat="server" 
onselectedindexchanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" AutoGenerateSelectButton="true">

<Columns>

<asp:BoundFieldDataField="ID"HeaderText="ID"SortExpression="ID" />

<asp:BoundFieldDataField="Name"HeaderText="Name"SortExpression="Name" />

<asp:BoundFieldDataField="Address"HeaderText="Address" SortExpression="Address" />

</Columns>

</asp:GridView>

<asp:SqlDataSourceID="SqlDataSource1"runat="server" ConnectionString="<
;%$ ConnectionStrings:PersonConnectionString %>" 
SelectCommand="SELECT * FROM [PersonInfo]"></asp:SqlDataSource>
</p>

<p>
<asp:labelid="lblMessage"forecolor="Red"runat="server"/>
</p>



Code behind for selectedIndexChanged Event:






protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

{
 
  GridViewRow row = GridView1.SelectedRow;
  
  lblMessage.Text = "Selected" + row.Cells[2].Text + ".";
}