Click here to Skip to main content
11,410,174 members (61,143 online)
Rate this: bad
good
Please Sign up or sign in to vote.
I have the following database design:

Employee Table: Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode
Divisions Table: SapCode, DivisionShortcut


And I have a GridView that I am using it to add, delete and update/edit the employees information. This information is employee Username, Name, BadgeNo, JobTitle, IsActive and the DivisionShortcut. ***IsActive is a flag that indicates if the employee is available or in an assignment***. I made it as a checkbox and the column should show two values; Active and Inactive. In the Edit mode, the Checkbox will be displayed. If it is checked, then it means the employee is avaiable, otherwise it is inactive.

I wrote the code and everything works fine, but now I am facing two problems and I don't know how to make them work with my code.

1. The GridView shows True or False instead of Active and Inactive and I don't know why.
2. In the Edit mode, the checkbox will be displayed and the values (Active or Inactive) will be displayed besides to it and it shouldn't be displayed. I just want the checkbox to be displayed.


**So how to modify them?**


ASP.NET code:
    <%-- GridView for User Management Subsystem --%>
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
            AutoGenerateColumns="False" DataKeyNames="Username" 
            DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" BorderWidth="1px" BackColor="#DEBA84" 
             CellPadding="3" CellSpacing="2" BorderStyle="None" 
             BorderColor="#DEBA84">
            <FooterStyle ForeColor="#8C4510" 
              BackColor="#F7DFB5"></FooterStyle>
            <PagerStyle ForeColor="#8C4510" 
              HorizontalAlign="Center"></PagerStyle>
            <HeaderStyle ForeColor="White" Font-Bold="True" 
              BackColor="#A55129"></HeaderStyle>
            <Columns>
                <asp:CommandField ButtonType="Image" ShowEditButton="true" ShowCancelButton="true"
                                EditImageUrl="Images/icons/edit24.png" UpdateImageUrl="Images/icons/update24.png" 
                                CancelImageUrl="Images/icons/cancel324.png" />
 
                <asp:TemplateField HeaderText="Division">
                    <ItemTemplate>
                        <%# Eval("DivisionShortcut")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DivisionsList" runat="server" DataSourceID="DivisionsListDataSource"
                                          DataTextField="DivisionShortcut" DataValueField="SapCode"
                                          SelectedValue='<%# Bind("DivisionCode")%>'>
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
 
                <asp:BoundField DataField="Username" HeaderText="Network ID" ReadOnly="True" 
                    SortExpression="Username" />
                    
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <%# Eval("Name")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEmployeeName" runat="server" Text='<%# Bind("Name")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
 
                <asp:TemplateField HeaderText="Job Title">
                    <ItemTemplate>
                        <%# Eval("JobTitle")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtJobTitle" runat="server" Text='<%# Bind("JobTitle")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
 
                <asp:TemplateField HeaderText="Badge No.">
                    <ItemTemplate>
                        <%# Eval("BadgeNo")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtBadgeNo" runat="server" Text='<%# Bind("BadgeNo")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
 
                <asp:TemplateField HeaderText="Is Active?">
                    <ItemTemplate>
                        <asp:Label ID="lblIsActive" runat="server" Text='<%# Eval("IsActive")%>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="isActive" runat="server" 
                                      AutoPostBack="true" OnCheckedChanged="isActive_OnCheckedChanged"
                                      Checked='<%# Convert.ToBoolean(Eval("IsActive")) %>'
                                      Text='<%# Eval("IsActive")%>'/>
                    </EditItemTemplate>
                </asp:TemplateField>
                
                <asp:TemplateField HeaderText="Delete?">
                    <ItemTemplate>
                        <span  önclick="return confirm('Are you sure to Delete the record?')">
                            <asp:ImageButton ID="lnkB" runat="server" ImageUrl="Images/icons/delete24.png" CommandName="Delete" />
                        </span>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Code-behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // here you will select the IsActive column and modify it's text
        Label isActive=new Label();
        isActive = (Label) e.row.FindControl("lblIsActive");
        // after getting the reference set its text
        isActive.Text="Active or InActive";            
 
    }
}
 
    //for updating the (IsActive) column using checkbox inside the GridView
        protected void isActive_OnCheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkStatus = (CheckBox)sender;
            GridViewRow gvrow = (GridViewRow)chkStatus.NamingContainer;
    
            //Get the ID which is the NetworkID of the employee
            string username = gvrow.Cells[2].Text;
            bool status = chkStatus.Checked;
    
            string connString = ConfigurationManager.ConnectionStrings["UsersInfoDBConnectionString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
    
            string updateIsActive = "UPDATE Employee SET IsActive = @IsActive WHERE Username = @Username";
    
            SqlCommand cmd = new SqlCommand(updateIsActive, conn);
    
            cmd.Parameters.AddWithValue("@IsActive", status);
            cmd.Parameters.AddWithValue("@Username", username);
    
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException se)
            {
                throw se;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }
Posted 15-Jul-12 18:58pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,

1. For this, you will need to modify your stored procedure to , if it is returning True then make in Active for False return Inactive. Alternatively if you do not wish to make any changes in stored procedure, you can as well make changes in RowDataBound event, by checking the value of IsActive . If it is true, display the label text as "Active" or vice versa.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // here you will select the IsActive column and modify it's text
        Label isActive =(Label) e.row.FindControl("lblIsActive");
        // after getting the reference set its text
        if(isActive.Text.ToLower() == "true")
            isActive.Text = "Active";
        else
            isActive.Text = "Inactive";
    }
}




2.Answer to your second question: Remove below line of code from EditItemTemplate in your gridview
 Text='<%# Eval("IsActive")%>'/>
  Permalink  
v2
Comments
AshishChaudha at 16-Jul-12 0:55am
   
Good answer.my 5!
matrix388 at 16-Jul-12 1:33am
   
thanks for your help. However, I removed that part from the EditItemTemplate and nothing is changes. Also, Regarding the values 'Active' or 'Inactive', could you please show me how to do it in the RowDataBound method?
Vani Kulkarni at 16-Jul-12 2:39am
   
I have updated the answer. Please see if it works.
matrix388 at 16-Jul-12 2:42am
   
It works now but when I click on the Edit, I got the following error and I don't know why:
System.NullReferenceException: Object reference not set to an instance of an object.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 159
1 Maciej Los 68
2 Philippe Mori 60
3 BillWoodruff 60
4 OriginalGriff 60
0 Sergey Alexandrovich Kryukov 8,755
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web01 | 2.8.150414.5 | Last Updated 16 Jul 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100