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
开发者_StackOverflow 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?
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
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] .....
精彩评论