Monday, December 9, 2013

copy data from 1 datatable to another

There are multiple ways to copy data from 1 datatable to another.
a)dtnew=dtold.copy();
copies both structure and data
b)dtnew=dtold,clone()
copies only structure.
(we can use it instead of adding columns)

c)
to copies data of specific columns

    DataTable dtnew=new DataTable();
            dtnew.Columns.Add("sl_no");
            dtnew.Columns.Add("Tenant");

            foreach(DataRow dr in dt.Rows)
            {
                DataRow drNew = dtnew.NewRow();
                drNew["sl_no"]=dr["slno"];
                drNew["Tenant"] = dr["tenant_name"];
                dtnew.Rows.Add(drNew);


            }

Saturday, September 14, 2013

Adding/using accordian in VS2010

Creating according requires following steps to be followed
a) downlaod ajax control kit dll .
b)add ajax control kit in toolbox

now add script manager
<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>



 add accordian control
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    </asp:UpdatePanel>
<asp:Accordion ID="Accordion1" runat="server" RequireOpenedPane="false" HeaderSelectedCssClass="accordionHeaderSelected" SelectedIndex="0" AutoSize="None" FadeTransitions="true" TransitionDuration="300" FramesPerSecond="25" Height="500px">
<Panes>
<asp:AccordionPane ID="Pane1" runat ="server" HeaderCssClass="accordionHeader" >

<Header >
<b>This is a header</b>


</Header>
<Content>

This is content1. just  created for ajax testing
</Content>

</asp:AccordionPane>

<asp:AccordionPane ID="pane2" runat="server" HeaderCssClass="accordionHeader" >
<Header>
<b>This is a header2</b>
</Header>
<Content>
This is Content2.
</Content>
</asp:AccordionPane>
</Panes>
</asp:Accordion>

run the solution

screenshot shown below

Thursday, September 12, 2013

Capturing Template field values of editable Gridview

I was capturing the values from the grid view . i could do it oretty easily but what struck me was how do i capture values of a template field in editable grid view. it was returning null.
so here is what I did

a)Use find control method with rowdatabound() event.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl= e.Row.FindControl("Tenant_Name") as Label;
               
            }

}

b)in the Rowcommand()event i looped through each row . and at specified row index i captured the desired value a below:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
               string str= GridView1.Rows[i].Cells[1].Text;
            }
        }

here is the screenshot


Wednesday, September 11, 2013

Sorting a Grid column on header click

i had a small requirement to sort a grid column when column header is clicked .
Here is what i did

a) Enable allowsorting Property in grid-view

<asp:GridView ID="GridView1" runat="server" CellPadding="3" GridLines="Horizontal"
   Font-Names="Verdana" Font-Size="10" DataKeyNames="Tenant_Name"
   AutoGenerateColumns="false" AllowSorting="true"
        onselectedindexchanged="GridView1_SelectedIndexChanged"
        onrowcancelingedit="GridView1_RowCancelingEdit"
        onrowcreated="GridView1_RowCreated" onrowediting="GridView1_RowEditing"
        onrowupdated="GridView1_RowUpdated" onrowupdating="GridView1_RowUpdating"
        onrowdeleted="GridView1_RowDeleted" onrowdeleting="GridView1_RowDeleting"
        onrowdatabound="GridView1_RowDataBound" onsorting="GridView1_Sorting" 

   >

add Property SortExpression

      <asp:BoundField DataField="Tenant_Name" HeaderText="Tenant Name" ReadOnly="true" SortExpression="Tenant_Name" />



b) Create a Property  for sort direction and store in a view state

  public SortDirection dir

    {
        get

        {

            if (ViewState["dirState"] == null)

            {

                ViewState["dirState"] = SortDirection.Ascending;

            }

            return (SortDirection)ViewState["dirState"];

        }
        set


        {

            ViewState["dirState"] = value;

        }

    }


c) if data is stored in dataset copy dataset to datatable

 dt = dsshowTen.Tables[0];

d) add event  GridView1_Sorting(object sender, GridViewSortEventArgs e) event 

   
            string sortingDirection = string.Empty;

        if (dir == SortDirection.Ascending)

        {

            dir = SortDirection.Descending;

            sortingDirection = "Desc";

        }

        else

        {

            dir = SortDirection.Ascending;

            sortingDirection = "Asc";

        }

       


        DataView sortedView = new DataView(dt);

        sortedView.Sort = e.SortExpression + " " + sortingDirection;

        GridView1.DataSource = sortedView;

        GridView1.DataBind();





the above 2 screenshot shows sorted records of Tenant_Name column