[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6251
  • Last Modified:

Using GridViewRow DataBound Event Args

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
hoyaabanks
Asked:
hoyaabanks
  • 2
  • 2
1 Solution
 
surajgupthaCommented:
e.Row.RowIndex == gvSections.Rows.Count
0
 
hoyaabanksAuthor Commented:
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
 
surajgupthaCommented:
>>  I want to make a button disappear on teh first and last records in the gridview.  
Can you explain this in detail please?
0
 
CtrlAltDlCommented:
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
 
CtrlAltDlCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now