Urgent - DataGrid

E

Ed

Hi,
Is there anyway to add a command button at the end of each row and when i
click on it, it can post one of the value into another field?
e.g.
SS# LastName FirstName
111111111 John Smith CommandButton
222222222 Mary Kay CommandButton

When I click on CommandButton, i want to have the SS# to put into a textbox.
Thanks
Ed
 
K

Ken Cox [Microsoft MVP]

Hi Ed,

This should do what you need. Let us know?

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


Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("SS", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("LastName", GetType(String)))
dt.Columns.Add(New DataColumn _
("FirstName", GetType(String)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i + i
dr(1) = "LastName " + i.ToString()
dr(2) = "FirstName " + i.ToString()
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Private Sub DataGrid1_ItemCommand _
(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls. _
DataGridCommandEventArgs) _
Handles DataGrid1.ItemCommand
If e.CommandName = "Select" Then
TextBox1.Text = e.Item.Cells(0).Text
End If
End Sub


<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="SS" ReadOnly="True"
HeaderText="SS"></asp:BoundColumn>
<asp:BoundColumn DataField="Lastname" ReadOnly="True" HeaderText="Last
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="First
Name"></asp:BoundColumn>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>


Ken
Microsoft MVP [ASP.NET]
 
E

Ed

Ken,
Thanks for your answer, but the error occurs when I run it...
on the line of DataGrid1.ItemCommand -- Handles clause require a WithEvent.
What's wrong?

Also, I want to make sure when I click on the "Select" button on the grid,
the Private Sub DataGrid1_ItemCommand will be fired off?

Thanks again
Ed
Ken Cox said:
Hi Ed,

This should do what you need. Let us know?

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


Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("SS", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("LastName", GetType(String)))
dt.Columns.Add(New DataColumn _
("FirstName", GetType(String)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i + i
dr(1) = "LastName " + i.ToString()
dr(2) = "FirstName " + i.ToString()
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Private Sub DataGrid1_ItemCommand _
(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls. _
DataGridCommandEventArgs) _
Handles DataGrid1.ItemCommand
If e.CommandName = "Select" Then
TextBox1.Text = e.Item.Cells(0).Text
End If
End Sub


<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="SS" ReadOnly="True"
HeaderText="SS"></asp:BoundColumn>
<asp:BoundColumn DataField="Lastname" ReadOnly="True" HeaderText="Last
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="First
Name"></asp:BoundColumn>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>


Ken
Microsoft MVP [ASP.NET]



Ed said:
Hi,
Is there anyway to add a command button at the end of each row and when i
click on it, it can post one of the value into another field?
e.g.
SS# LastName FirstName
111111111 John Smith
CommandButton
222222222 Mary Kay
CommandButton

When I click on CommandButton, i want to have the SS# to put into a
textbox.
Thanks
Ed
 
K

Ken Cox [Microsoft MVP]

Oops. I assumed you would have the declarations... the designer inserted if
for me. It looks like this:

Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

Here's the complete code from my VS.NET project. Your names will vary:

Public Class dgcmndbtn
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 TextBox1 As System.Web.UI.WebControls.TextBox

'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_ItemCreated _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemCreated
Dim hlnk As HyperLink
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
hlnk = e.Item.Cells(0).Controls(0)
hlnk.Attributes.Add("onmouseover", _
"alert('hello');")
End If
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("SS", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("LastName", GetType(String)))
dt.Columns.Add(New DataColumn _
("FirstName", GetType(String)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i + i
dr(1) = "LastName " + i.ToString()
dr(2) = "FirstName " + i.ToString()
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


End Class

<%@ Page Language="vb" AutoEventWireup="false" trace="true"
Codebehind="dgcmndbtn.aspx.vb" Inherits="p4320work.dgcmndbtn"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>dgcmndbtn</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>
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:HyperLinkColumn HeaderText="Description" DataNavigateUrlField="SS"
DataNavigateUrlFormatString="BillTypeDetail.aspx?BillTypeID={0}"
DataTextFormatString="{0:c}"
DataTextField="Lastname"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="SS" ReadOnly="True"
HeaderText="SS"></asp:BoundColumn>
<asp:BoundColumn DataField="Lastname" ReadOnly="True" HeaderText="Last
Name"></asp:BoundColumn>
<asp:BoundColumn DataField="FirstName" HeaderText="First
Name"></asp:BoundColumn>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
</form>
</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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top