Có datagridview trong biểu mẫu hiển thị nội dung của bảng cơ sở dữ liệu, một cột kiểu bảng là boolean, vì vậy trong datagridview hiển thị đúng/sai, nhưng tôi muốn tùy chỉnh nó hiển thị Có/Không. theo cách bạn đề xuất?hiển thị Yes/NO thay vì True/False trong datagridview
Trả lời
Khi nói đến định dạng tùy chỉnh, hai giải pháp khả dĩ có trong tâm trí của tôi.
1.Handle CellFormatting
sự kiện và định dạng của riêng bạn.
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == yourcolumnIndex)
{
if (e.Value is bool)
{
bool value = (bool)e.Value;
e.Value = (value) ? "Yes" : "No";
e.FormattingApplied = true;
}
}
}
2.Sử dụng Custom Formatter
public class BoolFormatter : ICustomFormatter, IFormatProvider
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
{
return this;
}
return null;
}
public string Format(string format, object arg, IFormatProvider formatProvider)
{
if (arg == null)
{
return string.Empty;
}
bool value = (bool)arg;
switch (format ?? string.Empty)
{
case "YesNo":
{
return (value) ? "Yes" : "No";
}
case "OnOff":
{
return (value) ? "On" : "Off";
}
default:
{
return value.ToString();//true/false
}
}
}
}
Sau đó sử dụng nó như thế này, và xử lý CellFormatting
sự kiện để làm cho nó hoạt
dataGridView1.Columns[1].DefaultCellStyle.FormatProvider = new BoolFormatter();
dataGridView1.Columns[1].DefaultCellStyle.Format = "YesNo";
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.CellStyle.FormatProvider is ICustomFormatter)
{
e.Value = (e.CellStyle.FormatProvider.GetFormat(typeof(ICustomFormatter)) as ICustomFormatter).Format(e.CellStyle.Format, e.Value, e.CellStyle.FormatProvider);
e.FormattingApplied = true;
}
}
Sửa Bạn có thể đăng ký để CellFormatting
sự kiện như này
dataGridView1.CellFormatting += dataGridView1_CellFormatting;
Hy vọng điều này sẽ giúp
này 'CellFormatting' sự kiện khi occures? Tôi muốn hiển thị Có/Không luôn! –
@MiladSobhkhiz không chắc chắn ý bạn là gì, bạn phải đăng ký sự kiện 'CellFormatting' nó sẽ xảy ra mỗi lần trước khi hiển thị giá trị cho lưới. kiểm tra chỉnh sửa của tôi. –
Làm thế nào về điều này nếu bạn chỉ muốn hiển thị. Thật dễ dàng để suy nghĩ.
private void Form1_Load(object sender, EventArgs e)
{
List<Person> list = new List<Person>();
list.Add(new Person(20, true));
list.Add(new Person(25, false));
list.Add(new Person(30, true));
dgv.DataSource = list;
//Hide checkbox column
dgv.Columns["IsProgrammer"].Visible = false;
//Add represent text column
DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn();
textColumn.Name = "Yes/No";
dgv.Columns.Add(textColumn);
//true/false -> yes/no
foreach (var row in dgv.Rows.Cast<DataGridViewRow>())
row.Cells["Yes/No"].Value = (bool)row.Cells["IsProgrammer"].Value ? "Yes" : "No";
}
private class Person
{
public int Age { get; set; }
public bool IsProgrammer { get; set; }
public Person(int i, bool b)
{
Age = i;
IsProgrammer = b;
}
}
void dataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
var grid = (DataGridView)sender;
if (grid.Columns[e.ColumnIndex].Name == "IsActive")
{
e.Value = (bool)e.Value ? "True_Text_Replace" : "False_Text_Replace";
e.FormattingApplied = true;
}
}
thấy http://stackoverflow.com/questions/9914411/replace-true-false-in-datagridview-columns?rq=1 – Shoe