Auto Ad Code

Showing posts with label Gridview. Show all posts
Showing posts with label Gridview. Show all posts

Tuesday, March 5, 2013

Datagrid/GridView Paging not working if set(initialize) from codebehind

Okay, here is another frustrating issue which you may face while using Datagrid or Gridview and you want to set (initialize) paging from code behind.
Your code will looks perfect to you but still paging is not functional.
Your code may look like
C#

  gvUsers.DataSource = mDtUsers; //mDtUsers is your datasource.
  gvUsers.DataBind();

  gvUsers.AllowPaging = true;
  gvUsers.PageSize = mPageSize;
  gvUsers.CurrentPageIndex = 0;


VB.Net

  gvUsers.DataSource = mDtUsers 'mDtUsers is your datasource.
  gvUsers.DataBind()

  gvUsers.AllowPaging = True
  gvUsers.PageSize = mPageSize
  gvUsers.CurrentPageIndex = 0


Everything looks fine? Yeh it do but still paging not functional. So whats the solution?

The Solution

Solution is pretty easy and straight forward and in fact you don't have to add any extra line of code, you simply have to change the sequence of statements. That means set (initialize) the paging before the binding of Datagrid/Gridview and everything will be working perfect, it means your code should be like

C#

  gvUsers.AllowPaging = true;
  gvUsers.PageSize = mPageSize;
  gvUsers.CurrentPageIndex = 0;

  gvUsers.DataSource = mDtUsers; //mDtUsers is your datasource.
  gvUsers.DataBind();



VB.Net

  gvUsers.AllowPaging = True
  gvUsers.PageSize = mPageSize
  gvUsers.CurrentPageIndex = 0

  gvUsers.DataSource = mDtUsers 'mDtUsers is your datasource.
  gvUsers.DataBind()


Hope this simnply solution will solve your problem.
Happy Coding! :)


Tuesday, December 4, 2012

[SOLVED] Dropdownlist SelectedIndexChanged (OnSelectedIndexChanged) From GridView

You may want to implement some functionality on changing index of a drop down list which is provided within a grid for multi records. I too had to do the same and searched over internet for some suitable solution. Though there are more than one solutions but the solution I am providing is most easy of them and requires no tricks.
The Solution
Note the RED BOLD code of HTML view, which means you will have to set EnableViewState="false" for the griview and also you will have to set AutoPostBack="true" for the dropdownlist.

Here is your GridView code (The HTML)


<asp:GridView runat="server" ID="gvTest" Width="50%" AutoGenerateColumns="false" EnableViewState="false">
            <Columns>
                <asp:BoundField DataField="Gender" HeaderText="Gender" HeaderStyle-HorizontalAlign="Left" />
                <asp:TemplateField >
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlTest" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddlTest_SelectedIndexChanged">
                            <asp:ListItem Value="Male">Male</asp:ListItem>
                            <asp:ListItem Value="Female">Female</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


And here is Code behind 
1. C#

protected void Page_Load(object sender, System.EventArgs e)
{
       try {
              FillGridView();
       } catch (Exception ex) {
              Response.Write(ex.Message);
       }
}

private void FillGridView()
{
       try {
              DataTable dt = new DataTable();
              dt.Columns.Add("Gender");
              DataRow dr = null;
              dr = dt.NewRow();
              dr["Gender"] = "I am a ";
              dt.Rows.Add(dr);

              gvTest.DataSource = dt;
              gvTest.DataBind();
       } catch (Exception ex) {
              Response.Write(ex.Message);
       }
}

protected void ddlTest_SelectedIndexChanged(object sender, System.EventArgs e)
{
       try {
              Response.Write("You selected " + ((DropDownList)sender).SelectedItem.Value);
       } catch (Exception ex) {
              Response.Write(ex.Message);
       }
}



2. VB.Net

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            FillGridView()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Private Sub FillGridView()
        Try
            Dim dt As New DataTable
            dt.Columns.Add("Gender")
            Dim dr As DataRow
            dr = dt.NewRow
            dr("Gender") = "I am a "
            dt.Rows.Add(dr)

            gvTest.DataSource = dt
            gvTest.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Protected Sub ddlTest_SelectedIndexChanged(sender As Object, e As System.EventArgs)
        Try
            Response.Write("You selected " & CType(sender, DropDownList).SelectedItem.Value)
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub


Wednesday, September 28, 2011

Gridview and Hidden Cells (Visible ="false")

This is another frustrating thing developers face while using Gridview instead of the Datagrid. That is when they try to use the value of some hidden cell of Gridview in Code behind they get the empty value. This is obviously very frustrating and have teased me a lot but after Googling and the doing some experiments I got the solution. Whats the solution?
Okay here is the simple solution for this issue
Don't set the Visible property equal to false in design time (HTML code). Instead set visibility to false in RowDataBound event of the Gridview. This will hide the cell but still the value of cell will be available in code behind.
Here is the sample code (let us suppose the Gridview id is gvTest)

VB.Net

Protected Sub gvTest_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvTest.RowDataBound
        e.Row.Cells(0).Visible = False
    End Sub


C#
protected void gvRecruiter_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = False
}



This will make Cells[0] to false but still its value will be available in code behind which will not be empty now.