How to get the data display value in Datagrid?

Discussion in 'ASP .Net Datagrid Control' started by JL, Dec 13, 2003.

  1. JL

    JL Guest

    Hi all

    My datagrid design

    Item_no, Item_name, Size
    1 ABC S
    2 BCD M

    *Item_no is Button Column

    I want to get the size value in specific row when i click the item_no button
    on same row, for example, i click the "item_no" - "2", this will return the
    value "M".

    Is this possible in Datagrid? If yes, how can do it in coding?

    Thanks for everybody
     
    JL, Dec 13, 2003
    #1
    1. Advertising

  2. Hi Joe,

    When you click on an item the row, the datagrid fires an ItemCommand event
    and sends along the row details in the event arguments. You can use that
    information to get an instance of the datagrid row and from it the contents
    of any cell. In the code below, I create an instance of the TableCell object
    calling it cellSize. I happen to know that within the grid row, the content
    I'm after is in the 2 row (zero-based). Therefore, I can extract the text
    and do what I want with it.

    I've included the complete source code below for your dining and dancing
    pleasure.

    Private Sub DataGrid1_ItemCommand _
    (ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
    Handles DataGrid1.ItemCommand
    Dim cellSize As TableCell
    cellSize = e.Item.Cells(2)
    Label1.Text = cellSize.Text
    End Sub

    Does this help?

    Ken



    Public Class dgimgclick
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    If Not IsPostBack Then
    DataGrid1.DataSource = CreateDataSource()
    DataGrid1.DataBind()
    End If
    End Sub

    Private Sub DataGrid1_ItemCommand _
    (ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
    Handles DataGrid1.ItemCommand
    Dim cellSize As TableCell
    cellSize = e.Item.Cells(2)
    Label1.Text = cellSize.Text
    End Sub

    Function CreateDataSource() As ICollection
    Dim dt As New DataTable
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn("Item_no", GetType(Int32)))
    dt.Columns.Add(New DataColumn("Item_name", GetType(String)))
    dt.Columns.Add(New DataColumn("Size", GetType(String)))
    Dim i As Integer
    For i = 0 To 8
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    If i > 7 Then
    dr(2) = "L"
    ElseIf i < 7 And i > 3 Then
    dr(2) = "M"
    Else
    dr(2) = "S"
    End If

    dt.Rows.Add(dr)
    Next i
    Dim dv As New DataView(dt)
    Return dv
    End Function 'CreateDataSource


    End Class



    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="dgimgclick.aspx.vb" Inherits="p4320work.dgimgclick"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>dgfooter</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="FlowLayout">
    <form id="Form1" method="post" runat="server">
    <asp:DataGrid id="DataGrid1" runat="server" ShowFooter="True"
    AutoGenerateColumns="False">
    <Columns>
    <asp:ButtonColumn DataTextField="Item_no" HeaderText="Item Number"
    CommandName="itemnum"></asp:ButtonColumn>
    <asp:BoundColumn DataField="Item_name" ReadOnly="True" HeaderText="Item
    Name"></asp:BoundColumn>
    <asp:BoundColumn DataField="Size" HeaderText="Size"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>
    </form>
    <P>&nbsp;</P>
    <P>
    <asp:Label id="Label1" runat="server">Label</asp:Label></P>
    </body>
    </HTML>



    "JL" <> wrote in message
    news:...
    > Hi all
    >
    > My datagrid design
    >
    > Item_no, Item_name, Size
    > 1 ABC S
    > 2 BCD M
    >
    > *Item_no is Button Column
    >
    > I want to get the size value in specific row when i click the item_no

    button
    > on same row, for example, i click the "item_no" - "2", this will return

    the
    > value "M".
    >
    > Is this possible in Datagrid? If yes, how can do it in coding?
    >
    > Thanks for everybody
    >
    >
     
    Ken Cox [Microsoft MVP], Dec 16, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JL
    Replies:
    3
    Views:
    2,393
  2. Skowronek
    Replies:
    4
    Views:
    4,566
    ProgTalk
    Dec 4, 2007
  3. John Dalberg
    Replies:
    0
    Views:
    392
    John Dalberg
    Dec 20, 2005
  4. John Dalberg

    How to get key value in a master datagrid from a detail datagrid?

    John Dalberg, Dec 20, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    148
    John Dalberg
    Dec 20, 2005
  5. jahles

    DataGrid - modifying data value for display

    jahles, Sep 12, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    124
    jahles
    Sep 12, 2003
Loading...

Share This Page