Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a array of strings as book-1,book-2,book-3,book-4,......

i want to show the above strings as in tooltip of datagridview in two columns

book-1 book-4
book-2 book-5
book-3 book-6


As so far,

i had spitted the strings was able to display as in a single columns

book-1
book-2
book-3
book-4

Any helps or hints would be appreciated. :)
Posted
Updated 21-Aug-11 6:22am
v2
Comments
Sergey Alexandrovich Kryukov 21-Aug-11 12:16pm    
What's the problem? What's the concern about "format"? Please use "Improve question".
--SA
Oludayo Alli 21-Aug-11 12:19pm    
Your question is not clear enough....Do you want to display each string in the array separately or combined? If combined, then in what format?
sanomama 21-Aug-11 12:21pm    
i want all the strings combined but in two columns as

book-1 book-4
book-2 book-5
book-3 book-6

In order to this u have to do a bit of formatting

white spaces are not so helpful

in order to implement the text in two columns u need to work out how many tabs u need to add between two columns of each row. Because 1st cel of 1st row might be too short and same of 1nd row might b too long. So just adding one tab in between two columns of each row will end up some this kind of formatting

b1 book1
gudbooks bok2
Books braveBooks
dontumindit myStuff
haythatscooliloveit Really

the reason is that 1st column of each row differs in length
to over come this problem, u need to work out whats the maximum length of the text which is going to be added, and for each row u need to work out how many tabs to be addedd

C#
private void Form1_Load(object sender, EventArgs e)
{
    //strings with different lengths
    string[] arr = { "b1", "book1", "gudbooks", "bok2", "Books", "braveBooks", "dontumindit", "myStuff", "haythatscool,iloveit", "Really" };
    int maxlen = 0;

    // calculating maximum length only for 1st cell items
    // that is items on all even places in array
    for (int i = 0; i < arr.Length; i++)
    {
        if (i % 2 == 0)
            if (maxlen < arr[i].Length)
                maxlen = arr[i].Length;
    }
    MessageBox.Show(maxlen.ToString());
    // Minimum No of Tabs which wil be added to maximum length cell
    int minimumTabSpace = 2;

    //getting Number of Tabs To overcome maxlength text
    int noOftabs = (maxlen / 8) + minimumTabSpace;
    string ToolTipText = "";

    for (int i = 0; i < arr.Length; i++)
    {
        ToolTipText += arr[i];
        if (i % 2 != 0)
        {
            //Insering New Line After every Two Cells
            ToolTipText += Environment.NewLine;
        }
        else
        {
            //Determining no. of Tabs for each Row
            //and inserting after 1st cell of each Row
            for (int j = 0; j < noOftabs - (arr[i].Length / 8); j++)
                ToolTipText += "\t";
        }
    }
    toolTip1.SetToolTip(button1, ToolTipText);
}


if u have too many white spaces in the text, then u need to work more to get proper allignment.

Best of Luck.
 
Share this answer
 
Comments
sanomama 21-Aug-11 15:17pm    
i had solved with int i %2 way :)
thanks for the reply
dontumindit 21-Aug-11 15:51pm    
welcome :)
Use ToolTip.SetToolTip(Control, string). One idea to remove some possible confusing is: a single tool tip instance set tool tip text for more than one instance of a Control.

See code samples on MDSN help page:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.settooltip.aspx[^].

—SA
 
Share this answer
 
Comments
sanomama 21-Aug-11 12:59pm    
i had used the set tooltip tooltip property bt was unable to show in two columns. Thanks anyway :)
I can't think of a better way now but this will sure help:

C#
string ms = nyArray.ElementAt(0) + " " + nyArray.ElementAt(3) + Environment.NewLine +
                nyArray.ElementAt(1) + " " + nyArray.ElementAt(4) + Environment.NewLine +
                nyArray.ElementAt(2) + " " + nyArray.ElementAt(5);

ToolTip tt = new ToolTip();
tt.Show(ms, this.datagridview);


The code above will produce:
sanomama wrote:
book-1 book-4

book-2 book-5

book-3 book-6



Note: There may be a better way to do this type of formatting, just google "String Formatting in C#"
 
Share this answer
 
v2
Comments
sanomama 21-Aug-11 12:59pm    
thanks.
Oludayo Alli 21-Aug-11 14:05pm    
If you're satisfied with the solution, you can atleast accept the solution so that the question will be mark 'SOLVED'
sanomama 21-Aug-11 16:10pm    
actually the strings in my Array r dynamic. I gues this process won't work. but i really appreciate your time and help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900