locked
Eval Column Value RRS feed

  • Question

  • User-1499457942 posted

    Hi

      In a Gridview i have field which has value 0 & 1 . I want if 0 it should display True else False

    Thanks

    Thursday, November 15, 2018 3:23 PM

Answers

  • User-1716253493 posted
    <%# (!Convert.ToBoolean(eval("fieldname"))).ToString() %>'

    Ensure, all rows has a value

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2018 12:24 AM
  • User-893317190 posted

    Hi JagjitSingh,

    If you want to use dropdownlist to  show the value.You could use the code below.

     <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
                <Columns>
                 
                    <asp:TemplateField HeaderText="boolean">
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                                <asp:ListItem>you haven't selected</asp:ListItem>
                                <asp:ListItem Value="0" Text="true"></asp:ListItem>
                                <asp:ListItem Value="1" Text="false"></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

    Code behind.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    object boolValue = DataBinder.Eval(e.Row.DataItem, "boolean");
                   if (boolValue!=DBNull.Value)
                    {
                        DropDownList dropDown= e.Row.FindControl("DropDownList1") as DropDownList; 'get the dropdownlist
                            if ((Int32)boolValue == 1)  'set the value according to the field with value (1,0)
                        {
                            dropDown.SelectedValue = "1"; 
                      
                        }
                        else
                        {
                            dropDown.SelectedValue = "0";
                         
                        }
                    }
                }
            }

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2018 5:32 AM

All replies

  • User-1716253493 posted
    <%# (!Convert.ToBoolean(eval("fieldname"))).ToString() %>'

    Ensure, all rows has a value

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2018 12:24 AM
  • User-893317190 posted

    Hi JagjitSingh,

    If you use BoundField, you could also try OnRowDataBound event.

    Below is my code.

     <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="boolean" HeaderText="boolean" />
                </Columns>
            </asp:GridView>

    Code behind.

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dataTable = new DataTable();
                    dataTable.Columns.Add(new DataColumn("boolean", typeof(Int32)));
                    DataRow row = dataTable.NewRow();
                    row["boolean"] = 0;
                    dataTable.Rows.Add(row);
                    row = dataTable.NewRow();
                    row["boolean"] = 1;
                    dataTable.Rows.Add(row);
                    row = dataTable.NewRow();
                    row["boolean"] = DBNull.Value;
                    dataTable.Rows.Add(row);
                    GridView1.DataSource = dataTable;
                    GridView1.DataBind();
                }
               
    
    
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)   //choose DataRow  (exclude headerRow)
                {
                    object boolValue = DataBinder.Eval(e.Row.DataItem, "boolean"); //get the value of boolean field
                   if (boolValue!=DBNull.Value)     //ensure it is not null
                    { 
                        if ((Int32)boolValue == 1)     // change it to false if it equals to 1
                        {
                            e.Row.Cells[0].Text = "false";
                        }
                        else
                        {
                            e.Row.Cells[0].Text = "true";
                        }
                    }
                }
            }

    The result.

    Best regards,

    Ackerly Xu

    Friday, November 16, 2018 2:22 AM
  • User-1499457942 posted

    Hi

      I want on Aspx Page to check . If 0 then True else false be displayed in DropDown value.

    Thanks

    Friday, November 16, 2018 4:48 AM
  • User-1499457942 posted

    Hi Ackerly

      I want on Aspx Page to check . If 0 then True else false be displayed in DropDown value.

    Thanks

    Friday, November 16, 2018 4:49 AM
  • User-893317190 posted

    Hi JagjitSingh,

    If you want to use dropdownlist to  show the value.You could use the code below.

     <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
                <Columns>
                 
                    <asp:TemplateField HeaderText="boolean">
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                                <asp:ListItem>you haven't selected</asp:ListItem>
                                <asp:ListItem Value="0" Text="true"></asp:ListItem>
                                <asp:ListItem Value="1" Text="false"></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

    Code behind.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    object boolValue = DataBinder.Eval(e.Row.DataItem, "boolean");
                   if (boolValue!=DBNull.Value)
                    {
                        DropDownList dropDown= e.Row.FindControl("DropDownList1") as DropDownList; 'get the dropdownlist
                            if ((Int32)boolValue == 1)  'set the value according to the field with value (1,0)
                        {
                            dropDown.SelectedValue = "1"; 
                      
                        }
                        else
                        {
                            dropDown.SelectedValue = "0";
                         
                        }
                    }
                }
            }

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 16, 2018 5:32 AM