我正在尝试在DataGridView单元格中以多行显示文本。我不想使用Wrap模式,因为文本不是很冗长。我只想在第一行显示第一个单词,在下一个单词中显示第二个单词。请注意第二行的粗体文字。
姓名:abc
城市:xyz
我尝试使用Environment.NewLine和“\ r \ n”来完成它,但都不起作用。
private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow dgvrow in dgv.Rows)
{
if (dgv.CurrentCell.ColumnIndex == dgv.Columns["Name"].Index)
{
DataGridViewCell dgvcell = (DataGridViewCell)dgvrow.Cells["Name"];
string Name = dgvcell.Value.ToString();
string City = Name.Substring(Name.IndexOf("City:"));
Name = Name.Substring(0, Name.IndexOf("City:")) + Environment.NewLine + City;
dgvcell.Value = Name;
}
}
}
有谁能建议如何实现这一目标?感谢。
答案 0 :(得分:3)
您应该可以通过将WrapMode
DefaultCellStyle
的{{1}}设置为 true 并设置DataGridViewTextBoxColumn
来实现此目的。
答案 1 :(得分:0)
试试这个.. 您需要将AutoSizeColumnsMode设置为AllCells。
foreach (DataGridViewRow dgvrow in dgv.Rows)
{
if (dgv.CurrentCell.ColumnIndex == dgv.Columns["Name"].Index)
{
DataGridViewCell dgvcell = (DataGridViewCell)dgvrow.Cells["Name"];
string Name = dgvcell.Value.ToString();
string City = Name.Substring(Name.IndexOf("City:"));
Name = Name+"\r\n"+City;
dgvcell.Value = Name;
}
}
它肯定会起作用
答案 2 :(得分:0)
我通过处理DataGridView_CellFormatting事件并根据需要格式化文本来修复它。谢谢大家的建议。
答案 3 :(得分:0)
这对我有用:
elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
tokens_input = [["the", "cat", "is", "on", "the", "mat"],
["dogs", "are", "in", "the", "fog", ""]]
tokens_length = [6, 5]
embeddings = elmo(
inputs={
"tokens": tokens_input,
"sequence_len": tokens_length
},
signature="tokens",
as_dict=True)["elmo"]
答案 4 :(得分:0)
我有一个类似的困难,因为我想使用换行,但是仅在我指定要换行符换行的地方使用。 我的解决方案是根据我不想包装的最长字符串调整列宽的大小,然后让包装完成其余的工作。
请随意尝试以下操作,或对其进行修改以适合您的目的。
private void dataGridView_TextItems_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (sender is DataGridView dgv && dgv.Rows[e.RowIndex].Cells[e.ColumnIndex] is DataGridViewTextBoxCell cell)
{
SetDGVTextBoxColumnWidth(dgv.Columns[e.ColumnIndex] as DataGridViewTextBoxColumn);
}
}
private void SetDGVTextBoxColumnWidth(DataGridViewTextBoxColumn column)
{
if (column != null)
{
DataGridView dgv = column.DataGridView;
Graphics g = dgv.CreateGraphics();
Font font = dgv.Font;
// Acquire all the relevant cells - the whole column's collection of cells:
List<DataGridViewTextBoxCell> cells = new List<DataGridViewTextBoxCell>();
foreach (DataGridViewRow row in column.DataGridView.Rows)
{
cells.Add(row.Cells[column.Index] as DataGridViewTextBoxCell);
}
// Now find the widest cell:
int widestCellWidth = g.MeasureString(column.HeaderText, font).ToSize().Width; // Start with the header text, but for some reason this seems a bit short.
bool foundNewline = false;
foreach (DataGridViewTextBoxCell cell in cells)
{
font = ((cell.Style.Font != null) ? cell.Style.Font : dgv.Font); // The font may change between cells.
string cellText = cell.Value.ToString().Replace("\r",""); // Ignore any carriage return characters.
if (cellText.Contains('\n'))
{
foundNewline = true;
cell.Style.WrapMode = DataGridViewTriState.True; // This allows newlines in the cell's text to be recognised.
string[] lines = cellText.Split('\n');
foreach (string line in lines)
{
int textWidth = g.MeasureString(line + "_", font).ToSize().Width; // A simple way to ensure that there is room for this text.
widestCellWidth = Math.Max(widestCellWidth, textWidth);
}
}
else
{
int textWidth = g.MeasureString(cellText + "_", font).ToSize().Width;
widestCellWidth = Math.Max(widestCellWidth, textWidth);
}
}
if (foundNewline)
{
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.None; // Allows us to programatically modify the column width.
column.Width = widestCellWidth; // Simply set the desired width.
}
else
{
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; // Allow the system to do the work for us. This does a better job with cell headers.
}
column.Resizable = DataGridViewTriState.False; // We don't wish the User to modify the width of this column manually.
}
}