I want to show database data in a grid view in C# by this code :
public void Selectalbums()
{
string QuerySelect = "select * from albums_tbl ";
SQLiteCommand cmd = new SQLiteCommand(QuerySelect, cn );
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView2.DataSource = dt;
}
now when I want to show a big string in a cell , it does not show compeletely ! how can I do this :
I set a maximum width for a column's cells (for exampele 250 px)and if the string is bigger than 250 px it will be shown in multiline mode ! I searched for it in stackoverflow and other sites but I did note find a solution
the width is Constante but when the stirring getting bigger this cell will be multiline
I'm using. NET Framework 3.5
I'm not sure how you are setting a column's maximum width. But if that maximum width is equivalent to the column's actual width, then the answer is simply:
dataGridView1.Columns[columnIndex].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dataGridView1.AutoResizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
As explained here, here, and numerous other places.
If they are not equivalent then you could have the following situations:
To handle it is pretty similar but on a cell by cell basis instead of the whole column. Below is an example:
private int column0MaxWidth = 100;
this.dataGridView1.Columns[0].Width = 55;
this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
// For contrast - column 1 contains the same data but is autosized.
this.dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
// On cell formatting, change a cell to single-lined or multilined.
void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewTextBoxCell cell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewTextBoxCell;
if (cell != null && e.ColumnIndex == 0)
{
cell.Style.WrapMode = TextRenderer.MeasureText(cell.Value.ToString(), cell.Style.Font).Width > column0MaxWidth ? DataGridViewTriState.True : DataGridViewTriState.NotSet;
}
}
This could easily be adapted for multiple columns depending how you set each column's max width.