Solved

Using GridViewRow DataBound Event Args

Posted on 2007-12-06
5
6,247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 21

Expert Comment

by:surajguptha
ID: 20422903
e.Row.RowIndex == gvSections.Rows.Count
0
 

Author Comment

by:hoyaabanks
ID: 20423293
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
ID: 20423513
>>  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
ID: 20424078
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
ID: 20424116
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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