【问题标题】:How to manipulate datagridview columns when using DataSource?使用 DataSource 时如何操作 datagridview 列?
【发布时间】:2011-08-13 12:01:20
【问题描述】:

我在 c# 上使用 datagridview 并用数据源对象提供给它。如果AutoGenerateColumnstrue,则一切正常,所有列都会生成其内容。但我只想显示第一列,并将其余列的信息用于第一列工具提示。

我尝试将AutoGenerateColumns 设置为false 并创建了一个名为的列,类似于AutoGenerateColumnstrue 时添加的第一列,但是当我这样做时没有添加任何行。

正确的做法是什么?

更新: 在我想隐藏的所有字段上方添加[Browsable(false)] :)

现在,我如何使用“隐藏”列中的数据并将它们用于第一列单元格工具提示?

【问题讨论】:

  • 正确回答这个问题确实需要一个代码示例——你确定你正确地连接了你的专栏吗?您需要将列上的 DataPropertyName 设置为您希望列使用的属性
  • 你在使用 Visual Studio 08/10 吗?

标签: c# datagrid datagridview datasource


【解决方案1】:

您可以在单元格格式化事件中执行此操作。这是来自 msdn 的代码 sn-p。

// 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

【讨论】:

    【解决方案2】:

    我认为你需要的是这样的:

    <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" />
    

    这样,您将在其中拥有列,您可以使用 Grid.Rows[selectedRow].Cells[index] .....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多