Saturday, March 24, 2012

Sort multiple column of a DataView in Asp.net C#




Please visit my new Web Site https://coderstechzone.com




In many case studies we found that when sorting is required developers done this job in back end means running extra query in database even though he has an already disconnected record set in his hand like DataView which also create an overhead to the application. One can easily sort the DataView columns in both ascending and descending order. Its very simple and for showing or displaying any type of sorted data in your report or details page you can do it without connecting to the Database through disconnected DataView. To define the sort direction we can use ASC or DESC keyword after the column name. For multiple column we just add comma separator for each column.







Code Example is given below:

// Create DataView from a DataTable Instance
DataView DV = datatable1.DefaultView;

// If you do not define sorting order then default Ascending order will be applied
DV.Sort = "ProductName ASC, CategoryName ASC, Price DESC";

For more details on DataTable or DataView CLICK HERE.

Thursday, March 1, 2012

Master page Group Radio Button problem Vertically in Gridview Control using Javascript in ASP.net C#




Please visit my new Web Site https://coderstechzone.com



In those cases where you need to choose or select one value from a list of values then grouping radio button is required. But unfortunately in ASP.Net there is no easiest built in way to group radio button vertically. But some times to meet client requirement we need to do group Radio Button vertically. It’s a grouping problem which most of the developer experience at least once in his student life or development life. Here i will try to write a javascript function to resolve Radio Button grouping problem within Gridview rows. It will also work in master page as well as in non master page. If you need to grouping Horizontally then read my THIS POST.

Output:
Problem in Grouping Radio Button

To do that we need to write a Javascript which will ensure single selection from a set of Radio Buttons in the following way:
1. First we need to know which Radio Button is clicked
2. Pass the selected Radio Button reference to the Javascript Function
3. Loop through Radio Button Array and set checked=false for others
4. Now you will get only one selected Radio Button at a time in your whole Gridview control

The complete HTML Markup is:
<script type="text/javascript">
        function GridSelection(objType)
        {
            var oItem = objType.children;
            var SelectedCtrl=(objType.type=="radio")?objType:objType.children.item[0];
            bChecked=SelectedCtrl.checked;
            arrRButtons=SelectedCtrl.form.elements;
            for(i=0;i<arrRButtons.length;i++)
            if(arrRButtons[i].type=="radio" && arrRButtons[i].id!=SelectedCtrl.id)
                arrRButtons[i].checked=!bChecked;
        }
    </script>

    <b>Who is your favourite player:</b><br />
    <asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" HorizontalAlign="Left">
        <Columns>
        <asp:boundfield datafield="Name" headertext="Super Player" />
        <asp:templatefield headertext="Choose">
            <ItemTemplate>
                <asp:radiobutton runat="server" id="chkChoose" onclick="javascript:GridSelection(this);"/>
            </ItemTemplate>
        </asp:templatefield>
        </Columns>
    </asp:GridView>
    <asp:Button runat="server" ID="cmdGet" Text="Get Selected Value" OnClick="cmdGet_Click" /><br />
    <asp:Literal runat="server" ID="ltrl"></asp:Literal>

The complete Codebehind Code is:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtPlayers = new DataTable("Super_Player");
            dtPlayers.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
            dtPlayers.Columns.Add(new DataColumn("Name"));
            dtPlayers.Rows.Add(1, "Leonel Messi");
            dtPlayers.Rows.Add(2, "Christiano Ronaldo");
            dtPlayers.Rows.Add(3, "Carlos Tevez");
            dtPlayers.Rows.Add(4, "Xavi");
            dtPlayers.Rows.Add(5, "Iniesta");
            GridView1.DataSource = dtPlayers;
            GridView1.DataBind();
        }
    }

    protected void cmdGet_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow oRow in GridView1.Rows)
        {
            if (((RadioButton)oRow.FindControl("chkChoose")).Checked)
                ltrl.Text = "Selected ID = " + GridView1.DataKeys[oRow.RowIndex].Value + "
 Selected Name = " + oRow.Cells[0].Text;
            // Now You can do update or delete or anything.................. Based on selection
        }
    }

Hope now you can apply single selection or group Radio Button in your Master Pages as well as normal pages. The javascirpt code is tested for Internet Explorer, Mozila Firefox, Opera, Google Chrome etc.

Leave your comment if can not implement.

Happy coding.

Read DataKeyNames value of Gridview row using Asp.net C#




Please visit my new Web Site https://coderstechzone.com



When we need to bind data in a Gridview column then we need to add an unique reference number which will help to identify a specific object or a datarow. Some developers use hidden field to store the ID column of a table on which he can INSERT UPDATE or DELETE based on hidden ID column. But this is not a good practice. Because Gridview control gives us a property named DataKeyNames on which we can assign our unique ID or code column value to distinguish invidual row. Here in this example i will show how one can read DataKeyNames value from code behind. Asp.net Gridview control also provides us a facility to assign more ID or Code type column in a single Gridview for each DataRow item specialy for composite keys of a table to bind. I have already discuss "How to use more DataKeynames in a Gridview". Also i have discuss on "Jquery to read DatakeyNames value of Selected Rows of GridView".

Sample Output:
Read Datakeynames of a Gridview row

HTML Markup Code:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" HorizontalAlign="Left">
        <Columns>
            <asp:boundfield datafield="Name" headertext="Super Player" />
        </Columns>
    </asp:GridView>

Codebehind Code:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtPlayers = new DataTable("Super_Player");
            dtPlayers.Columns.Add(new DataColumn("ID", System.Type.GetType("System.UInt64")));
            dtPlayers.Columns.Add(new DataColumn("Name"));
            dtPlayers.Rows.Add(1, "Leonel Messi");
            dtPlayers.Rows.Add(2, "Christiano Ronaldo");
            dtPlayers.Rows.Add(3, "Carlos Tevez");
            dtPlayers.Rows.Add(4, "Xavi");
            dtPlayers.Rows.Add(5, "Iniesta");
            GridView1.DataSource = dtPlayers;
            GridView1.DataBind();

            foreach (GridViewRow oRow in GridView1.Rows)
                Response.Write("DataKeyNames="+GridView1.DataKeys[oRow.RowIndex].Value + "
");
        }
    }
Hope now you can use DataKeyNames property of a Gridview control efficiently when required.

How to get week number from a date in SQL Server




Please visit my new Web Site https://coderstechzone.com



As a developer i beleive that most of the times we need to built our query based on date column. Some times we need to get or find out the week number from a given date. Sql server provides us such an easy way to find out the week number from a date. The function is DATEPART. By using DATEPART function we can calculate week number easily. Please follow my example code to achieve the expected result.

Sample Output:
TSQL_DATEPART Function Example


To run the example find the following code:
CREATE TABLE [dbo].[Employee]
(
 [ID] [int] NULL,
 [Name] [varchar](200) NULL,
 [JoiningDate] [smalldatetime] NULL
)

INSERT INTO EMPLOYEE VALUES(1,'Shawpnendu','Jan 01, 2012')
INSERT INTO EMPLOYEE VALUES(2,'Bimalandu','Jan 10, 2012')
INSERT INTO EMPLOYEE VALUES(3,'Purnendu','Jan 20, 2012')
INSERT INTO EMPLOYEE VALUES(4,'Amalendu','Jan 30, 2012')
INSERT INTO EMPLOYEE VALUES(5,'Chadbindu','Feb 05, 2012')

SELECT *,DATEPART(wk,JoiningDate) [Week Number] FROM EMPLOYEE

Hope now you can retrieve week number from a given date using DATEPART TSQL function.
Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash