How to get the data display value in Datagrid?

J

JL

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
 
K

Ken Cox [Microsoft MVP]

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top