1

I am using datagridview on c# and im feeding it with a datasource object. If AutoGenerateColumns is true, everything works fine and all the columns gets generated with their content. But I want to display only the 1st column, and use the info on the rest of the columns for the 1st column tooltip.

I tried setting AutoGenerateColumns to false and created a column called like the 1st column added when AutoGenerateColumns is true, but no rows are being added when I do this.

Whats the right way to do it?

Update: Adding [Browsable(false)] above all the fields i want to hide worked :)

Now, how could I use data from the "hidden" columns and use them for the 1st column cells tooltips?

2
  • 1
    A code sample is really required to answer this question correctly -- are you sure you hooked up your column correctly? You need to set DataPropertyName on the column to the property you want the column to use
    – debracey
    Commented Apr 27, 2011 at 2:07
  • Are you using visual studio 08/10?
    – clamchoda
    Commented Apr 27, 2011 at 2:38

2 Answers 2

3

You can do this in the cell formatting event. Here is a code snippet from msdn.

// Sets the ToolTip text for cells in the Rating column.
void dataGridView1_CellFormatting(object sender, 
    DataGridViewCellFormattingEventArgs e)
    {
    if ( (e.ColumnIndex == this.dataGridView1.Columns["Rating"].Index)
        && e.Value != null )
    {
        DataGridViewCell cell = 
            this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        if (e.Value.Equals("*"))
        {                
            cell.ToolTipText = "very bad"; // you can get the value from your other cells using the above technique with .value instead of .index
        }
        else if (e.Value.Equals("**"))
        {
            cell.ToolTipText = "bad";
        }
        else if (e.Value.Equals("***"))
        {
            cell.ToolTipText = "good";
        }
        else if (e.Value.Equals("****"))
        {
            cell.ToolTipText = "very good";
        }
    }
}

More info here

0
0

I think what you need is something like this:

<asp:GridView ID="gridStatus" runat="server" AutoGenerateColumns="False"  DataSourceID="SqlDataSourceForStatusGrid">
    <Columns>
        <asp:BoundField DataField="ID" Visible =false />
        <asp:BoundField DataField="StudentName" HeaderText="Name" SortExpression="StudentName" />

This way you will have the column in there and you can retrive it use Grid.Rows[selectedRow].Cells[index] .....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.