Solved

Using GridViewRow DataBound Event Args

Posted on 2007-12-06
5
6,235 Views
Last Modified: 2013-11-26
The following method is called when my gridview is databound.  I want to make a button disappear on teh first and last records in the gridview.  The code below only works for the first.  The commented out line needs to be fixed to get it to work for the last.

How can I test to see if this is the last row being added to the gridview?

    protected void gvSections_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton up = (ImageButton)e.Row.FindControl("btMoveUp");
            ImageButton down = (ImageButton)e.Row.FindControl("btMoveDown");
            up.Visible = true;
           
            down.Visible = true;
            if (e.Row.RowIndex == 0) up.Visible = false;
            //else if (e.Row.RowIndex > 3) down.Visible = false;
        }
    }
0
Comment
Question by:hoyaabanks
  • 2
  • 2
5 Comments
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
e.Row.RowIndex == gvSections.Rows.Count
0
 

Author Comment

by:hoyaabanks
Comment Utility
gvSection.Rows.Count always equals the same as RowIndex.  I think the reason for this is that the rows are currently being created when this event handler is called.

I need something that gives me all of the rows that are goign to be generated.  Is there a way to count the dataItem rows?
0
 
LVL 21

Expert Comment

by:surajguptha
Comment Utility
>>  I want to make a button disappear on teh first and last records in the gridview.  
Can you explain this in detail please?
0
 
LVL 14

Expert Comment

by:CtrlAltDl
Comment Utility
I think you need to do a seperate function (getCount) that does a SQL query to get the record counts.  Then you change your code to this:
else if (e.Row.RowIndex == getCount()) down.Visible = false;
    public static int getCount(string strUserName)

    {

        int intCount = 0;

        string strSQL = "SELECT count(empID) FROM tblEmployees WHERE Login = '" + strUserName + "'";

        string strConn = ConfigurationManager.ConnectionStrings["myConnectionString"].ToString();
 

        SqlConnection objConn = new SqlConnection(strConn);
 

        SqlCommand objCmd = new SqlCommand(strSQL, objConn);
 

        objConn.Open();
 

        SqlDataReader objReader = objCmd.ExecuteReader();
 

        if (objReader.HasRows)

        {

            while (objReader.Read())

            {

                intCount = (int)objReader[0].ToString();

            }

        }

        objReader.Close();

        objConn.Close();

        return intCount;

    }

Open in new window

0
 
LVL 14

Accepted Solution

by:
CtrlAltDl earned 500 total points
Comment Utility
Actually I think it should be one less since the row count is a zero based index:

else if (e.Row.RowIndex == getCount() - 1) down.Visible = false;

Of course you'll have to change the SQL to be the same as your datasource for the GridView.  You'll also need to change the ConnectionString too.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now