Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Urgent - DataGrid

Reply
Thread Tools

Urgent - DataGrid

 
 
Ed
Guest
Posts: n/a
 
      08-19-2004
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
 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      08-19-2004
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


<aspataGrid 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>
</aspataGrid>
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>


Ken
Microsoft MVP [ASP.NET]



"Ed" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Ed
Guest
Posts: n/a
 
      08-20-2004

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 [Microsoft MVP]" wrote:

> 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
>
>
> <aspataGrid 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>
> </aspataGrid>
> <P>
> <asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
>
>
> Ken
> Microsoft MVP [ASP.NET]
>
>
>
> "Ed" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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

>
>

 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      08-20-2004
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">
<aspataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:HyperLinkColumn HeaderText="Description" DataNavigateUrlField="SS"
DataNavigateUrlFormatString="BillTypeDetail.aspx?B illTypeID={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>
</aspataGrid>
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
</form>
</body>
</HTML>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT RichA <rander3127@gmail.com> For rec.photo.digital MVP of all time. Vote now!! URGENT. Griffin Digital Photography 0 08-27-2010 08:18 AM
urgent help....need urgent help on say string task.. pooja Software 0 03-03-2009 06:16 AM
!!URGENT!! Tor Vulnerability Discovered !!URGENT!! Security Advisory Computer Security 1 08-11-2007 09:04 PM
URGENT !! QUEUE STL PROBLEM URGENT!! Sachin Jagtap C++ 4 04-29-2005 12:08 PM
Urgent problem, repeat, Urgent problem. Rob Javascript 3 05-18-2004 12:05 AM



Advertisments