开发者

How to manipulate datagridview columns when using DataSource?

开发者 https://www.devze.com 2023-03-01 02:43 出处:网络
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 dis

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] .....

0

精彩评论

暂无评论...
验证码 换一张
取 消