making two different style of datagridview's one cell
-
hey guys..i want to change wordS' styles in a single cell of my datagridview for example in a cell i have "AsdAsd.CvbCvb"..i want it to make as AsdAsd CvbCvb..to do that i declared two cellstyle
DataGridViewCellStyle mystyle1 = new DataGridViewCellStyle();
DataGridViewCellStyle mystyle2 = new DataGridViewCellStyle();mystyle1.Font = new Font(dataGridView1.Font, FontStyle.Regular);
mystyle2.Font = new Font(dataGridView1.Font, FontStyle.Italic);and to get the words in my cells i made this
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
string split = dataGridView1.Rows[i].Cells[j].Value.ToString();
string[] split2 = split.Split(new char[] { '.' });foreach (string item in split2) { dataGridView1.Rows\[i\].Cells\[j\].Value = split2\[0\].ToString()+"/\*/\*" + split2\[1\].ToString(); } } }
but the point is that i dont know how to apply that styles to the words i got by splitting? can anyone suggest me a way
-
hey guys..i want to change wordS' styles in a single cell of my datagridview for example in a cell i have "AsdAsd.CvbCvb"..i want it to make as AsdAsd CvbCvb..to do that i declared two cellstyle
DataGridViewCellStyle mystyle1 = new DataGridViewCellStyle();
DataGridViewCellStyle mystyle2 = new DataGridViewCellStyle();mystyle1.Font = new Font(dataGridView1.Font, FontStyle.Regular);
mystyle2.Font = new Font(dataGridView1.Font, FontStyle.Italic);and to get the words in my cells i made this
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
string split = dataGridView1.Rows[i].Cells[j].Value.ToString();
string[] split2 = split.Split(new char[] { '.' });foreach (string item in split2) { dataGridView1.Rows\[i\].Cells\[j\].Value = split2\[0\].ToString()+"/\*/\*" + split2\[1\].ToString(); } } }
but the point is that i dont know how to apply that styles to the words i got by splitting? can anyone suggest me a way
I think you can create two classes, one inheriting from DataGridViewColumn and the other one from DataGridViewCell, where you can use a RichTextBox control to show your data with the format you need. I have never done this, but I guess it is not too hard. Read the documentation about these two classes. On the other hand, I would like to help you improve your code: All of these lines (I have added some comments):
string split = dataGridView1.Rows[i].Cells[j].Value.ToString();
string[] split2 = split.Split(new char[] { '.' }); // split.Split('.') is better// This loop is useless. Yoy can remove it
foreach (string item in split2)
{
// Are you sure split2 array has two or more items?
// and since split2 is a string[], there is no need of ToString() for split2[0] and split2[1]
dataGridView1.Rows[i].Cells[j].Value = split2[0].ToString()+"/*/*" + split2[1].ToString();
}Give the same result as:
dataGridView1[j,i].Value = dataGridView1[j,i].Value.ToString().Replace(".", "/*/*");
But there is a difference: if you do not have a "." char, your code would also throw an exception.
-
hey guys..i want to change wordS' styles in a single cell of my datagridview for example in a cell i have "AsdAsd.CvbCvb"..i want it to make as AsdAsd CvbCvb..to do that i declared two cellstyle
DataGridViewCellStyle mystyle1 = new DataGridViewCellStyle();
DataGridViewCellStyle mystyle2 = new DataGridViewCellStyle();mystyle1.Font = new Font(dataGridView1.Font, FontStyle.Regular);
mystyle2.Font = new Font(dataGridView1.Font, FontStyle.Italic);and to get the words in my cells i made this
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
string split = dataGridView1.Rows[i].Cells[j].Value.ToString();
string[] split2 = split.Split(new char[] { '.' });foreach (string item in split2) { dataGridView1.Rows\[i\].Cells\[j\].Value = split2\[0\].ToString()+"/\*/\*" + split2\[1\].ToString(); } } }
but the point is that i dont know how to apply that styles to the words i got by splitting? can anyone suggest me a way
As far as I know there is no way to do this with the 'standard'
DataGridViewTextBoxColumn
orDataGridViewTextBoxCell
. You can do it with theDataGridViewRichTextBoxColumn
or Cell. RichTextBox Cell in a DataGridView[^], might help.Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
hey guys..i want to change wordS' styles in a single cell of my datagridview for example in a cell i have "AsdAsd.CvbCvb"..i want it to make as AsdAsd CvbCvb..to do that i declared two cellstyle
DataGridViewCellStyle mystyle1 = new DataGridViewCellStyle();
DataGridViewCellStyle mystyle2 = new DataGridViewCellStyle();mystyle1.Font = new Font(dataGridView1.Font, FontStyle.Regular);
mystyle2.Font = new Font(dataGridView1.Font, FontStyle.Italic);and to get the words in my cells i made this
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
string split = dataGridView1.Rows[i].Cells[j].Value.ToString();
string[] split2 = split.Split(new char[] { '.' });foreach (string item in split2) { dataGridView1.Rows\[i\].Cells\[j\].Value = split2\[0\].ToString()+"/\*/\*" + split2\[1\].ToString(); } } }
but the point is that i dont know how to apply that styles to the words i got by splitting? can anyone suggest me a way
You can't use the styles because they apply to the ENTIRE cell, not just one little piece of content in it. You have to write your own custom DataGridViewColumn implementation to pull this off. There are examples out there, you just have to Google for something like "Custom DataGridViewColumn" for them.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
You can't use the styles because they apply to the ENTIRE cell, not just one little piece of content in it. You have to write your own custom DataGridViewColumn implementation to pull this off. There are examples out there, you just have to Google for something like "Custom DataGridViewColumn" for them.
A guide to posting questions on CodeProject[^]
Dave Kreskowiakthanks for your asnwers guys..i will look at the examples and search more deeper.. because iam not that much professional yet to create a Custom DataGridView