C# 使用DataSource时如何操作datagridview列?
我在c#上使用datagridview,并向它提供一个数据源对象。如果C# 使用DataSource时如何操作datagridview列?,c#,datagrid,datagridview,datasource,C#,Datagrid,Datagridview,Datasource,我在c#上使用datagridview,并向它提供一个数据源对象。如果AutoGenerateColumns为true,则一切正常,所有列都会与其内容一起生成。但我只想显示第1列,并将其余列的信息用于第1列工具提示 我尝试将AutoGenerateColumns设置为false,并创建了一个名为的列,就像在AutoGenerateColumns为true时添加的第一列一样,但这样做时没有添加行 正确的方法是什么 更新: 在我要隐藏的所有字段上方添加[Browsable(false)]:) 现在,
AutoGenerateColumns
为true
,则一切正常,所有列都会与其内容一起生成。但我只想显示第1列,并将其余列的信息用于第1列工具提示
我尝试将AutoGenerateColumns
设置为false
,并创建了一个名为的列,就像在AutoGenerateColumns
为true
时添加的第一列一样,但这样做时没有添加行
正确的方法是什么
更新:
在我要隐藏的所有字段上方添加[Browsable(false)]
:)
现在,我如何使用“隐藏”列中的数据并将其用于第一列单元格工具提示?我想您需要的是这样的:
<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]。下面是来自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";
}
}
}
正确回答这个问题确实需要一个代码示例——您确定您正确地连接了专栏吗?您需要将列上的DataPropertyName设置为希望该列使用的属性是否使用visual studio 08/10?