Sunday, 2 March 2014

Sorting asp.net GridView with SPDataSource

http://stackoverflow.com/questions/16027310/sorting-asp-net-gridview-with-spdatasource



Then I created my grid, based on the datasource
    <asp:GridView Visible="true" Width="100%"
                ID="gvInventar"
                AutoGenerateColumns="false" runat="server" CellPadding="2" AllowPaging="false" AllowSorting="true" OnSorted="gvInventar_Sorted" OnSorting="gvInventar_Sorting" GridLines="None" DataSourceID="SPdsInventarGrid" OnRowCommand="gvInventar_RowCommand">
                <Columns>
                    <asp:CommandField ButtonType="Image" ShowEditButton="true" EditImageUrl="/_layouts/images/edit.gif" UpdateImageUrl="/_layouts/images/save.gif" CancelImageUrl="/_layouts/images/delete.gif" />

                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <asp:Label Text='<%# Bind("ID") %>' runat="server" ID="lblIdProduct"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Title" >
                        <ItemTemplate>
                            <asp:Label Text='<%# Bind("Title") %>' runat="server" ID="lblTitleProduct" ></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Product" SortExpression="Product">
                        <ItemTemplate>
                            <asp:Label Text='<%# Bind("EBS_x002e_CN_x002e_Inventar_x0028") %>' runat="server" ID="lblProduct" "></asp:Label>
                        </ItemTemplate>

                </Columns>
    </asp:GridView>
Now, I have this function where I wanted to get the current Query as a string and add <OrderBy>(my field) before <Query> but i get an error saying that the DataSource has been already declared (which is true because i want to be declared in asp)



protected void gvInventar_Sorting(object sender, GridViewSortEventArgs e)
    {
        SPQuery q = new SPQuery();
        q.Query = SPdsInventarGrid.SelectCommand;
        switch (e.SortExpression)
        {
            case "Product":
                if (e.SortDirection == SortDirection.Ascending)
                {
                    gvInventar.DataSource = q.Query;
                    gvInventar.DataBind();
                }
                else
                {
                }
               break;
        }
    }


<Columns>
    <asp:TemplateField HeaderText="ID" SortExpression="ID">
        <ItemTemplate>
            <asp:Label Text='<%# Bind("ID") %>' runat="server" ID="lblIdProduct" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Title" SortExpression="Title">
        <ItemTemplate>
            <asp:Label Text='<%# Bind("Title") %>' runat="server" ID="lblTitleProduct" />
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Product" SortExpression="EBS_x002e_CN_x002e_Inventar_x0028">
        <ItemTemplate>
            <asp:Label Text='<%# Bind("EBS_x002e_CN_x002e_Inventar_x0028") %>' runat="server"
                ID="lblProduct" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
Now you can sort on all 3 columns:
enter image description here

Friday, 28 February 2014

Textbox string empty, caldander string empty after button click

<table width="450px" border="0" cellspacing="0" cellpadding="3" align="center">
          <tr>
            <td valign="top" class="text" align="center" >
                <asp:Label ID="Label1" runat="server" Text="empname"></asp:Label>
       </td>
            <td valign="top" class="text" align="center">
                <asp:TextBox ID="txtempname" runat="server" Height="22px"></asp:TextBox>
       </td>
     
          </tr>
          <tr>
            <td valign="top" class="text" align="center">
                <asp:Label ID="Label2" runat="server" Text="eno"></asp:Label>
       </td>
            <td valign="top" class="text" align="center">
                <asp:TextBox ID="txteno" runat="server"></asp:TextBox>
       </td>
     
          </tr>  
           <tr>
            <td valign="top" class="text" align="center">
                <asp:Label ID="Label3" runat="server" Text="startdate"></asp:Label>
       </td>
            <td valign="top" class="text" align="center">
                <asp:Calendar ID="empcalstartdate" runat="server"></asp:Calendar>
               
             
       </td>
     
          </tr>
           <tr>
            <td valign="top" class="text" align="center">
                &nbsp;</td>
            <td valign="top" class="text" align="center">
                &nbsp;</td>
     
          </tr>
           <tr>
            <td valign="top" class="text" align="center">
                <asp:Label ID="lblds" runat="server" Text="Your details submitted"
                    Visible="False"></asp:Label>
               </td>
            <td valign="top" class="text" align="center">
                <asp:Button ID="Submit" runat="server" BorderStyle="Solid" Text="Submit"
                    onclick="Submit_Click" />
                <asp:Button ID="btngraph" runat="server" BorderStyle="Solid" Text="Display graph"
                    onclick="Submit_Click"  Visible="False" />
       </td>
     
          </tr>          
        </table>  













protected void Submit_Click(object sender, EventArgs e)
        {
           

            //string empty1 = string.Empty;
            //empty1 = txtempname.Text;

            //string empty2 = string.Empty;
            //empty1 = txteno.Text;

            //string empty3 = string.Empty;
            //empty1 = empcalstartdate.SelectedDate.ToString();

            if (txtempname.Text.Length == 0 || txteno.Text.Length == 0)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Please type your User ID and Password correctly and click Submit button again.  Thanks", true);

            }

            else
            {

                SPSite site = SPContext.Current.Site;
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists["Employeeinformations"];
                web.AllowUnsafeUpdates = true;
                SPListItemCollection coll = list.Items;
                SPListItem item = coll.Add();
                item["empname"] = txtempname.Text;

                // string txtnostring = txteno.Text;
                //int m = Int32.Parse("txtno");

                item["eno"] = txteno.Text;

                item["empjoineddate"] = empcalstartdate.SelectedDate;
                web.AllowUnsafeUpdates = false;
                lblds.Visible = true;
                item.Update();
                list.Update();


                lblds.Text = string.Empty;
                txtempname.Text = string.Empty;
                txteno.Text = string.Empty;

                btngraph.Visible = true;

                string seldt = empcalstartdate.SelectedDate.ToString();
                seldt = string.Empty;



            }  
    }

Wednesday, 26 February 2014

Gridview Sorting

<asp:GridView ID="gvDetails" runat="server"
              AutoGenerateColumns="False"  AllowPaging="true"
              onsorting="gvDetails_Sorting" PageSize="6" AllowSorting="True">
<Columns>
<asp:TemplateField HeaderText="EmployeeName" SortExpression="EmployeeName">
<ItemTemplate>
<asp:Label ID="lblFname" runat="server" Text='<%#Eval("EmployeeName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile" SortExpression="Mobile">
<ItemTemplate>
<asp:Label ID="lblLname" runat="server" Text='<%#Eval("Mobile")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BatchCode" SortExpression="BatchCode">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("BatchCode")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created" SortExpression="Created">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Created")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>





 protected void Page_Load(object sender, EventArgs e)
        {
            gvDetails.DataSource = BindGridView();
            gvDetails.DataBind();
        }



 protected void gvDetails_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortingDirection = string.Empty;
            if (sortProperty == SortDirection.Ascending)
            {
                sortProperty = SortDirection.Descending;
                sortingDirection = "Desc";
            }
            else
            {
                sortProperty = SortDirection.Ascending;
                sortingDirection = "Asc";
            }

            DataView sortedView = new DataView(BindGridView());
            sortedView.Sort = e.SortExpression + " " + sortingDirection;
            gvDetails.DataSource = sortedView;
            gvDetails.DataBind();
        }


     private DataTable BindGridView()
        {
            SPWeb web = SPContext.Current.Web;
            SPList list = web.Lists["Employee Feedback Details"];
            SPListItemCollection item = list.GetItems();


            //foreach (SPListItem items in item) ;
            //{

            //}
            DataTable dt = new DataTable();
            dt = item.GetDataTable();

            return dt;
            //gvDetails.DataSource = dt;
            //gvDetails.DataBind();
        }



        public SortDirection sortProperty
        {
            get
            {
                if (ViewState["SortingState"] == null)
                {
                    ViewState["SortingState"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortingState"];
            }
            set
            {
                ViewState["SortingState"] = value;
            }
        }