boolean columns appear as yes/no in datagrdiview
You could do something like:
dgv.AutoGenerateColumns = false;
Manually type in the dataProperty name that the datagridView can map to and do
dgv.DataSource = mysource.Select(m=> new{Col1 = m.Property1,
Col2 = m.property2,
Col3 = ((m.property3) ? "Yes":"No")})
.ToList();
Your Col3 on your DataGridView will then need to be a text instead of a checkbox.
Show Yes/No instead of 0/1 in datagrid view for c#?
Assuming the data value is an integer as you say, you can use the CellFormatting
event of the DataGridView
control like this:
private void grid_CellFormatting(object sender, ataGridViewCellFormattingEventArgs e)
{
if ( e.ColumnIndex == columnIndex )
if ( e.Value is int )
e.Value = (int)e.Value == 0 ? "No" : "Yes";
}
All values other than 0 are considered true else you can modify the test as you want.
Replace columnIndex
by the index you need starting from 0.
Also replace the int
check and cast by long
if you use a SQLite x64 driver.
You can also use the column name like this:
if ( (sender as DataGridView)?.Columns[e.ColumnIndex].Name == "column name" )
Or the binded data property name from your query:
if ( (sender as DataGridView)?.Columns[e.ColumnIndex].DataPropertyName == "name" )
Using column index or name you are independant from the query.
Using data property name you are independant from the columns order and naming.
You may choose what you prefer.
replace true/false in datagridview columns
You can use the CellFormatting
event of the DataGridView
, e.g.:
void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
var grid = (DataGridView)sender;
if (grid.Columns[e.ColumnIndex].Name == "IsActive")
{
e.Value = (bool)e.Value ? "MY_TEXT_FOR_TRUE" : "MY_TEXT_FOR_FALSE";
e.FormattingApplied = true;
}
}
EDIT (as per comment):
It's very similar to what you're doing now, just remove the bound column and add a new column of the desired type and set the DataPropertyName
properly e.g. :
this.dataGridView1.Columns.Remove("COL_TO_CUSTOMIZE");
var btnCol = new DataGridViewDisableButtonColumn();
btnCol.Name = "COL_TO_CUSTOMIZE";
btnCol.DataPropertyName = "COL_TO_CUSTOMIZE";
var col = this.dataGridView1.Columns.Add(btnCol);
Note that this append the column at the end, but you can decide the position of the column by using dataGridView.Columns.Insert
method instead of Add
.
Parse boolean value to Yes/No in datagridview of vb.net windows forms application
if you have a SELECT statement maybe you may try a DECODE; for MS SQL it should be:
SELECT CASE WHEN FieldA = 'A' THEN 'myA' ELSE 'myB'
FROM myTable.
Can I convert a boolean to Yes/No in a ASP.NET GridView
I use this code for VB:
<asp:TemplateField HeaderText="Active" SortExpression="Active">
<ItemTemplate><%#IIf(Boolean.Parse(Eval("Active").ToString()), "Yes", "No")%></ItemTemplate>
</asp:TemplateField>
And this should work for C# (untested):
<asp:TemplateField HeaderText="Active" SortExpression="Active">
<ItemTemplate><%# (Boolean.Parse(Eval("Active").ToString())) ? "Yes" : "No" %></ItemTemplate>
</asp:TemplateField>
Show yes/no in bool column of gridview bound to datasource in Winforms?
I have encountered the same problem, unfortunately I didn't find an elegant solution.
Three workarounds are proposed:
- Add another property to the data source's class, which returns your string representation of the boolean property. Hide the column showing the boolean value, display the column showing the string value.
- Add an unbound string column, populate that column with the appropriate value for each row, and hide the boolean bound column.
- Create a wrapper class for your data class, which exposes the properties as you'd like them to be shown in the datagrid.
Bind Boolean values as yes no in datagrid text column in WPF
You could replace the DataGridTextColumn
with a DataGridTemplateColumn
:
<DataGridTemplateColumn Header="Active" CanUserResize="False" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="NO" />
<Style.Triggers>
<DataTrigger Binding="{Binding stuen.IsDelete}" Value="True">
<Setter Property="Text" Value="YES" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Related Topics
Write PDF Stream to Response Stream
Linq Query Group by and Selecting First Items
How to Retrieve the Screen Resolution from a C# Winform App
Share Session Between Two Web Sites Using ASP.NET and State Server
How to Convert an Array of Floats to a Byte[] and Back
How to Programmatically List All Projects in a Solution
Equivalent Code of Createobject in C#
Using Webclient or Webrequest to Login to a Website and Access Data
How to Add the Same Column to All Entities in Ef Core
Correlation of Two Arrays in C#
C#: Detecting Which Application Has Focus
Why Does a Bad Password Cause "Padding Is Invalid and Cannot Be Removed"