Urgent - DataGrid

Discussion in 'ASP .Net Datagrid Control' started by Ed, Aug 19, 2004.

  1. Ed

    Ed Guest

    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
    Ed, Aug 19, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
    Ken Cox [Microsoft MVP], Aug 20, 2004
    #2
    1. Advertising

  3. Ed

    Ed Guest

    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
    >
    >
    > <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" <> wrote in message
    > news:...
    > > 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

    >
    >
    Ed, Aug 20, 2004
    #3
  4. 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>
    Ken Cox [Microsoft MVP], Aug 20, 2004
    #4
    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. Vincent

    DataGrid questions (urgent)

    Vincent, Jul 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    444
    Saravana
    Jul 3, 2003
  2. Sachin Jagtap

    URGENT !! QUEUE STL PROBLEM URGENT!!

    Sachin Jagtap, Apr 28, 2005, in forum: C++
    Replies:
    4
    Views:
    1,040
    Mark Stijnman
    Apr 29, 2005
  3. Andreas Klemt
    Replies:
    0
    Views:
    293
    Andreas Klemt
    Oct 8, 2003
  4. Rob
    Replies:
    3
    Views:
    435
  5. Replies:
    1
    Views:
    263
Loading...

Share This Page