Friday, June 22

Different ways to Bind Data in a TextBox From Gridview Cells by Click on Select Button Using Both RowCommand And SelectedIndexChanged Event

Here i  am using two gridview to show you two different  ways to get data from gridview.

Following is my Table Structure which i am going to use for data binding in gridview:-















My Tables with DataType:-











My Table Data:-








Following is my Design View:-










Here is My Source code:-

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

<!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>Shibashish Mohanty</title>
</head>
<body>
    <form id="form1" runat="server">
  
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <div id="FirstStyle">
    <div>
    <h1>
    Select Data from gridview and Bind it in Textbox Using RowCommand</h1>
    </div>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false"
          
            onrowcommand="GridView2_RowCommand">
        <Columns>
        <asp:BoundField DataField="MenuID" HeaderText="MenuID" />
         <asp:BoundField DataField="Text" HeaderText="Text" />
          <asp:BoundField DataField="Description" HeaderText="Description" />

        </Columns>
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="LinkButton1" CommandName="shibashish" CommandArgument='<%# Eval("MenuID") %>'  runat="server">Select</asp:LinkButton>
        </ItemTemplate>
         </asp:TemplateField>
        
        </Columns>
        </asp:GridView>
   
    </div>
 <div id="SecondStyle">
    <div>
    <h1>
    Select Data from gridview and Bind it in Textbox Using
        SelectedIndexChanged </h1>
    </div>
        <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="false"
         AutoGenerateSelectButton="true"
         onselectedindexchanged="GridView3_SelectedIndexChanged" >
        <Columns>
        <asp:BoundField DataField="MenuID" HeaderText="MenuID" />
         <asp:BoundField DataField="Text" HeaderText="Text" />
          <asp:BoundField DataField="Description" HeaderText="Description" />
          <asp:TemplateField>
         </asp:TemplateField>
        </Columns>
       </asp:GridView>
   
    </div>
   
    </form>
</body>
</html>

Here is My Code Behind:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

public partial class Entitydatamodel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillBothGrid();
        }
    }
  
    #region First GridView With RowCommand Event

    public void FillBothGrid()
    {
        TestModel.TestEntities obj = new TestModel.TestEntities();
        GridView2.DataSource = from k in obj.Menus select k;
        GridView2.DataBind();
        GridView3.DataSource = from k in obj.Menus select k;
        GridView3.DataBind();
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //if (e.CommandName == "shibashish")
        //{
        GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

        TextBox1.Text = row.Cells[1].Text;

        //}


    }

    #endregion
#region Second GridView With SelectedIndexChanged Event

    protected void GridView3_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow gvr = GridView3.SelectedRow;
        TextBox1.Text = gvr.Cells[2].Text;
    }
    #endregion

}


After running it will display as;-













If we will click on the second row of second gridview it will display as














Thanks Shibashish Mohanty



1 comment:

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty