Page 1 of 1

Using GridView and RowCommand Events beginner guide to simple gridview navigation/events

#1 d_rop4nme  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 149
  • Joined: 18-April 10

Posted 03 August 2010 - 07:20 PM

So I get so much help around here I figure I gotta try to give a little back. Let me preface this by saying this is a beginners tutorial, also my first tutorial. Hope it helps someone.

In this tutorial I'm going to go over some simple common ways to use ASP gridview controls and the RowCommand to trigger an events and then to navigate to a detail form view.

Let get into it:

A simple way to navigate to a detailed view is to set up a gridview with data from your database:

Posted Image

If you don't know how to setup a gridview go here.

Create a event by double clicking your gridview, you should get an event like:


   protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }




This will be triggered whenever the user clicks your gridview, we can then use some code to redirect the user to a detail view:


   protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string RecordId = GridView1.SelectedRow.Cells[1].Text; 
        Session["RecordID"] = RecordId;
        Response.Redirect("DetailPage.aspx");
    }




Let's go line by line:

string RecordId = GridView1.SelectedRow.Cells[1].Text;
This bit of code will select the recordID which is in column 1 (remember were 0 based) I have chosen to leave this visible but you can hide the column in the markup by setting the column property visible = false

Session["RecordID"] = RecordId;
This will store our recordID into the Session Object so we can pick it up on the next page

Response.Redirect("CaseView.aspx");
This will redirect us to our our detail view page

So now were going to write some code in the pageload event of our detailpage.aspx page, this code will be executed when the page load's giving us the desired result.


protected void Page_Load(object sender, EventArgs e)
    {                       
        string RecordID2 = (string)(Session["RecordID"]);
    
        int RecID = System.Convert.ToInt32(RecordID2);
        DataClassesDataContext db = new DataClassesDataContext();
        var resultset = from r in db.Records
                        where r.RecordID == RecID
                        select r;



string RecordID2 = (string)(Session["RecordID"]);
int RecID = System.Convert.ToInt32(RecordID2);
Here we are picking up the RecordID that we stored into the Session Object converting it into an integer

DataClassesDataContext db = new DataClassesDataContext();
var resultset = from r in db.Records
where r.RecordID == RecID
select r;

Next we pass it into a LINQ statement to get the specific row of data.

*You could just as easily do this with ADO/SQL if you choose.
        foreach (var r in resultset)
        {

            Label3.Text = r.First + " " + r.Last;
            TextBox1.Text = r.First;
            TextBox16.Text = r.Last;
            TextBox2.Text = r.Address;
            TextBox3.Text = r.City;
  etc .................................
        }
    }


Here I am populating the fields on the form to give us something like

Posted Image

Note: This is a very simple example and there are often better/more secure ways to do this but this is good place to start if your trying to learn ASP ...

Now lets look at how to use the RowCommand Event. This is a way to initiate a more complex event from your gridview.

Visual:
Posted Image

ASP Markup

<asp:GridView ID="GridView1" runat="server" OnRowCommand="myGV_RowCommand" AutoGenerateColumns="False" 
                                onselectedindexchanged="GridView1_SelectedIndexChanged">
                                <AlternatingRowStyle BackColor="White" />
                                <Columns>
                                    <asp:ButtonField HeaderText="Download" 
                                         Text="Download" 
                                         ButtonType="Button" 
                                         CommandName="click1" />
                                    <asp:ButtonField ButtonType="Button" 
                                         HeaderText="Answer" 
                                         Text="Answer" 
                                         CommandName="click2" /> [/b]
                                    <asp:BoundField DataField="Doc_Txt" HeaderText="Type" 
                                        SortExpression="Doc_Txt" />

ect ... 




What am I looking at ?

Ok take it all in:
1. Our GridView is called, well GridView1
2. Our OnRowCommand is myGV_RowCommand
3. The CommandName forour buttons is Click1 and Click 2 Respectively

Next we have to write some code in our cs file to handle these two would be events:

protected void myGV_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "click1")
        {
            int index = Convert.ToInt32(e.CommandArgument); //get row number selected
            GridViewRow row = GridView1.Rows[index]; 
            Go ahead do something like above 

        }

        if (e.CommandName == "click2")
        {
            Do something cool ... 
        }
    }



:rockon:

Good link to check out:
GridView.RowCommand Event

This post has been edited by d_rop4nme: 04 August 2010 - 03:02 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Using GridView and RowCommand Events

#2 Tlmoz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-November 10

Posted 09 November 2010 - 01:19 AM

Thanks
Was This Post Helpful? 0
  • +
  • -

#3 Guest_emine*


Reputation:

Posted 23 December 2010 - 08:42 AM

View PostTlmoz, on 09 November 2010 - 12:19 AM, said:

Thanks a lot! i found exactly what i looking for

Was This Post Helpful? 0

#4 c0deBlu3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 25-March 11

Posted 25 March 2011 - 08:37 AM

Thank you!!!

I am new to this forum and new to ASP and VB programming. I have been trying to figure out how to utilize rows (databound) to access a specific page that we are working for our client.

Another issue is that within my grid I created there is a column for Status (the status of an ourder recieved) Within the DSS skeleton of the program if the product is pending it is under a case "P" ...if the product has been delivered (from what i gathered) he wants nothing to appear in the "Status" column but if it hasnt been shipped or isn't on the same shipment case "P" needs to send a link to fill that box.....any ideas on where i should begin?

:helpsmilie: lol

Thanks
-CB
Was This Post Helpful? 0
  • +
  • -

#5 loonymalik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-April 13

Posted 07 April 2013 - 12:31 PM

Thank you! this was a very helpful topic, i found the solution to my problem! cheers!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1