Tuesday, 27 August 2013

Load a grid view with user details where male as 1 and female as 0 and populate a row if it is female


  • Design a Page like

<fieldset>
                    <legend>Employee Details</legend>
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="5" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField HeaderText="Sl No">
                                <ItemTemplate>
                                    <%# Container.DataItemIndex + 1%>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="EmpId" HeaderText="Employee Id"></asp:BoundField>
                            <asp:BoundField DataField="Designation" HeaderText="Designation"></asp:BoundField>
                            <asp:BoundField DataField="EmailId" HeaderText="EmailId"></asp:BoundField>
                            <asp:BoundField DataField="PhoneNo" HeaderText="PhoneNo"></asp:BoundField>
                            <asp:BoundField DataField="M_Statis" HeaderText="M_Statis"></asp:BoundField>
                            <asp:TemplateField HeaderText="Gender">
                                <ItemTemplate>
                                    <asp:Label ID="lblGender" runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <%--<asp:BoundField DataField="Gender" HeaderText="Gender" ></asp:BoundField>--%>
                            <asp:TemplateField HeaderText="Actions">
                                <ItemTemplate>
                                    <asp:LinkButton ID="Edit" Text="Edit" runat="server" CommandArgument='<%#Bind("EmpId")%>'
                                        OnClick="EditUsers" />
                                    <asp:LinkButton ID="Delete" Text="Delete" runat="server" OnClick="Delete_Click" CommandArgument='<%#Bind("EmpId")%>' />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </fieldset>

  • write the below in codebehind


 Dim con As New SqlConnection("Data Source=DV004\SQLEXPR2;Initial Catalog=UREG;User ID=sa;Password=mit@123")

      
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
         con.Open()
        Dim cmd As New SqlCommand("select * from UserProfile", con)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        GridView1.DataSource = ds
        ViewState("dt") = ds.Tables(0)
        GridView1.DataBind()
     End If

  End Sub
Protected Sub gvLogged_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lbl As Label = TryCast(e.Row.FindControl("lblGender"), Label)
            If lbl IsNot Nothing Then
                ' Display Gender info.
                If DataBinder.Eval(e.Row.DataItem, "Gender") = "Male" Then
                    lbl.Text = "1"
                Else
                    lbl.Text = "0"
                    e.Row.BackColor = Drawing.Color.BurlyWood
                End If

            End If
        End If

    End Sub

Output:


(Or)

Using SQL Query

SELECT  EmpId, Designation, EmailId, PhoneNo, M_Statis,(CASE Gender WHEN 'Male' THEN '1' ELSE '0' END)
FROM userprofile

No comments:

Post a Comment

SQL SERVER – Disk Space Monitoring – Detecting Low Disk Space on Server

CREATE PROCEDURE [CSMSDVLP].[DiskSpaceMonitor] @mailProfile nvarchar(500), @mailto nvarchar(4000), @threshold INT, @logfile nvarchar(40...