关于DataGrid的一个小问题 我想在DataGrid最前面的那个column中加入所得到的记录的序号,请问怎么做?效果就像sql查询器得到查询数据后一样 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在DataGrid的ItemDataBound事件中加入如下代码private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.ItemIndex>-1) { e.Item.Cells[0].Text = (e.Item.DataSetIndex+1).ToString(); } } <asp:TemplateColumn HeaderText="序号"><HeaderStyle Width="1cm" CssClass="fixedHeaderTr"></HeaderStyle><ItemTemplate> <asp:Label id=lblxh runat="server" Text="<%# Container.ItemIndex+1 %>"> </asp:Label></ItemTemplate></asp:TemplateColumn> 楼上两位,谢谢阿,不过我忘记说了,我是windows应用程序。 参考这个,可以直接使用/// <summary> /// WinDataGrid 的摘要说明。 /// </summary> public class WinDataGrid : System.Windows.Forms.DataGrid { public WinDataGrid() { // // TODO: 在此处添加构造函数逻辑 // this.SelectionBackColor = Color.FromArgb(159, 169, 197); this.AlternatingBackColor = Color.WhiteSmoke; this.SelectionForeColor = SystemColors.ControlText; this.GridLineColor = Color.Silver; this.CaptionVisible = false; this.RowHeadersVisible = true; this.RowHeaderWidth = 32; this.BackgroundColor = SystemColors.Control; this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true); } public new int RowHeaderWidth { get { return base.RowHeaderWidth < 32 ? 32 : base.RowHeaderWidth; } set { base.RowHeaderWidth = value; } } DataGridTableStyle m_DataGridTableStyle; /// <summary> /// Default DataGridTableStyle /// </summary> public DataGridTableStyle DataGridTableStyle { get { if( m_DataGridTableStyle == null ) { m_DataGridTableStyle = new DataGridTableStyle(); m_DataGridTableStyle.SelectionBackColor = this.SelectionBackColor; m_DataGridTableStyle.AlternatingBackColor = this.AlternatingBackColor; m_DataGridTableStyle.SelectionForeColor = this.SelectionForeColor; m_DataGridTableStyle.GridLineColor = this.GridLineColor; m_DataGridTableStyle.RowHeadersVisible = this.RowHeadersVisible; m_DataGridTableStyle.RowHeaderWidth = this.RowHeaderWidth; } return this.m_DataGridTableStyle; } } /// <summary> /// 应用颜色 /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { try { foreach( DataGridTableStyle tableStyle in this.TableStyles ) { tableStyle.SelectionBackColor = DataGridTableStyle.SelectionBackColor; tableStyle.AlternatingBackColor = DataGridTableStyle.AlternatingBackColor; tableStyle.SelectionForeColor = DataGridTableStyle.SelectionForeColor; tableStyle.GridLineColor = DataGridTableStyle.GridLineColor; tableStyle.RowHeadersVisible = DataGridTableStyle.RowHeadersVisible; tableStyle.RowHeaderWidth = this.RowHeaderWidth; } base.OnPaint (e); // 设计模式 if( !this.DesignMode ) { //显示行号 if ( DisplayRowNumber && this.RowHeadersVisible && this.VisibleColumnCount > 0 ) { try { if (this.DataSource == null) return; int iRowNumStart = this.VertScrollBar.Value; int yPos = 0; int iRowNumEnd = iRowNumStart + this.VisibleRowCount; while(iRowNumStart < iRowNumEnd) { yPos = this.GetCellBounds(iRowNumStart++, 0).Y + 2; string strRowNum = string.Format(" {0}", iRowNumStart); e.Graphics.DrawString(strRowNum, this.Font, new System.Drawing.SolidBrush(this.ForeColor), 6, yPos); } } catch {} } // 重绘背景色 /* int xPos = 0; int iCurrentRowIndex = this.CurrentRowIndex; if( iCurrentRowIndex >= 0) { try { while( xPos < VisibleColumnCount ) { Rectangle rect = this.GetCellBounds(iCurrentRowIndex, xPos); int rectX = rect.X, rectY = rect.Y; e.Graphics.FillRectangle(new System.Drawing.SolidBrush(this.SelectionBackColor), rect); e.Graphics.DrawString(string.Format("{0}", this[iCurrentRowIndex, xPos]), this.Font, new System.Drawing.SolidBrush(this.SelectionForeColor), rect); xPos++; } } catch {} } */ } } catch // catch(Exception ex) { // throw ex; } } bool m_DisplayRowNumber = true; /// <summary> /// 是否显示行号 /// </summary> [Browsable(true), DefaultValue(true), Description("是否显示行号")] public bool DisplayRowNumber { get { return this.m_DisplayRowNumber; } set { this.m_DisplayRowNumber = value; } } /// <summary> /// 点击处理Bool列事件 /// </summary> /// <param name="e"></param> protected override void OnCurrentCellChanged(EventArgs e) { base.OnCurrentCellChanged (e); if( this.LastRowIndex != this.CurrentRowIndex ) { this.m_lastRowIndex = this.CurrentRowIndex; } // 获取当前选定cell的列 BUG CMMDB00082794 if ( this.TableStyles.Count > 0 && this.TableStyles[0].GridColumnStyles.Count > this.CurrentCell.ColumnNumber ) { DataGridBoolColumn boolColumn = (this.TableStyles[0].GridColumnStyles[this.CurrentCell.ColumnNumber] as DataGridBoolColumn); if( boolColumn != null ) { boolColumn.AllowNull = false; if( Convert.IsDBNull(this[this.CurrentCell]) || (Convert.ToBoolean(this[this.CurrentCell]) == false) ) { this[this.CurrentCell] = true; } else { this[this.CurrentCell] = false; } } } } int m_lastRowIndex = 0; /// <summary> /// 原始的 LastRowIndex /// </summary> public int LastRowIndex { get { return this.m_lastRowIndex; } } } html:<asp:BoundColumn DataField="XID" HeaderText="***"></asp:BoundColumn>aspx.cs文件:利用sql查询返回相应的字段string strSql = "select xid from 表名" c# 导出excle 中文乱码 英文数字正常 ListBox 的求助 项目做好了用 InstallShield 打包 报错 是不是水晶报表的问题注册问题啊? 做過的給點思路。好思路再加100,關於保存數據有代碼加200分。 datagrid问题?把数据库显示出来的某行通过textbox等控件列出来 VS2005和VS2008的一个典型问题 winform如何让一个textbox只能输入数字和字母 C#调用C++dll报错 关于GUID的一个奇怪的问题 关于c# GridView索引范围超出异常 画图+屏幕随鼠标自动滚动 求助~!如何把窗体控件得消息传递给 form ?
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex>-1)
{
e.Item.Cells[0].Text = (e.Item.DataSetIndex+1).ToString();
}
}
<HeaderStyle Width="1cm" CssClass="fixedHeaderTr"></HeaderStyle>
<ItemTemplate> <asp:Label id=lblxh runat="server" Text="<%# Container.ItemIndex+1 %>"> </asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
/// WinDataGrid 的摘要说明。
/// </summary>
public class WinDataGrid : System.Windows.Forms.DataGrid
{
public WinDataGrid()
{
//
// TODO: 在此处添加构造函数逻辑
// this.SelectionBackColor = Color.FromArgb(159, 169, 197);
this.AlternatingBackColor = Color.WhiteSmoke;
this.SelectionForeColor = SystemColors.ControlText;
this.GridLineColor = Color.Silver;
this.CaptionVisible = false;
this.RowHeadersVisible = true;
this.RowHeaderWidth = 32;
this.BackgroundColor = SystemColors.Control; this.SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
} public new int RowHeaderWidth
{
get
{
return base.RowHeaderWidth < 32 ? 32 : base.RowHeaderWidth;
}
set
{
base.RowHeaderWidth = value;
}
}
DataGridTableStyle m_DataGridTableStyle;
/// <summary>
/// Default DataGridTableStyle
/// </summary>
public DataGridTableStyle DataGridTableStyle
{
get
{
if( m_DataGridTableStyle == null )
{
m_DataGridTableStyle = new DataGridTableStyle(); m_DataGridTableStyle.SelectionBackColor = this.SelectionBackColor;
m_DataGridTableStyle.AlternatingBackColor = this.AlternatingBackColor;
m_DataGridTableStyle.SelectionForeColor = this.SelectionForeColor;
m_DataGridTableStyle.GridLineColor = this.GridLineColor; m_DataGridTableStyle.RowHeadersVisible = this.RowHeadersVisible;
m_DataGridTableStyle.RowHeaderWidth = this.RowHeaderWidth;
} return this.m_DataGridTableStyle;
}
} /// <summary>
/// 应用颜色
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
try
{
foreach( DataGridTableStyle tableStyle in this.TableStyles )
{
tableStyle.SelectionBackColor = DataGridTableStyle.SelectionBackColor;
tableStyle.AlternatingBackColor = DataGridTableStyle.AlternatingBackColor;
tableStyle.SelectionForeColor = DataGridTableStyle.SelectionForeColor;
tableStyle.GridLineColor = DataGridTableStyle.GridLineColor; tableStyle.RowHeadersVisible = DataGridTableStyle.RowHeadersVisible;
tableStyle.RowHeaderWidth = this.RowHeaderWidth;
} base.OnPaint (e); // 设计模式
if( !this.DesignMode )
{
//显示行号
if ( DisplayRowNumber && this.RowHeadersVisible && this.VisibleColumnCount > 0 )
{
try
{
if (this.DataSource == null) return;
int iRowNumStart = this.VertScrollBar.Value;
int yPos = 0;
int iRowNumEnd = iRowNumStart + this.VisibleRowCount;
while(iRowNumStart < iRowNumEnd)
{
yPos = this.GetCellBounds(iRowNumStart++, 0).Y + 2;
string strRowNum = string.Format(" {0}", iRowNumStart);
e.Graphics.DrawString(strRowNum, this.Font, new System.Drawing.SolidBrush(this.ForeColor), 6, yPos);
}
}
catch
{}
} // 重绘背景色
/*
int xPos = 0;
int iCurrentRowIndex = this.CurrentRowIndex;
if( iCurrentRowIndex >= 0)
{
try
{
while( xPos < VisibleColumnCount )
{
Rectangle rect = this.GetCellBounds(iCurrentRowIndex, xPos);
int rectX = rect.X, rectY = rect.Y;
e.Graphics.FillRectangle(new System.Drawing.SolidBrush(this.SelectionBackColor), rect); e.Graphics.DrawString(string.Format("{0}", this[iCurrentRowIndex, xPos]),
this.Font,
new System.Drawing.SolidBrush(this.SelectionForeColor),
rect); xPos++;
}
}
catch
{}
}
*/
}
}
catch // catch(Exception ex)
{
// throw ex;
}
} bool m_DisplayRowNumber = true;
/// <summary>
/// 是否显示行号
/// </summary>
[Browsable(true), DefaultValue(true), Description("是否显示行号")]
public bool DisplayRowNumber
{
get
{
return this.m_DisplayRowNumber;
}
set
{
this.m_DisplayRowNumber = value;
}
}
/// <summary>
/// 点击处理Bool列事件
/// </summary>
/// <param name="e"></param>
protected override void OnCurrentCellChanged(EventArgs e)
{
base.OnCurrentCellChanged (e); if( this.LastRowIndex != this.CurrentRowIndex )
{
this.m_lastRowIndex = this.CurrentRowIndex;
}
// 获取当前选定cell的列 BUG CMMDB00082794
if ( this.TableStyles.Count > 0 && this.TableStyles[0].GridColumnStyles.Count > this.CurrentCell.ColumnNumber )
{
DataGridBoolColumn boolColumn = (this.TableStyles[0].GridColumnStyles[this.CurrentCell.ColumnNumber] as DataGridBoolColumn); if( boolColumn != null )
{
boolColumn.AllowNull = false; if( Convert.IsDBNull(this[this.CurrentCell]) || (Convert.ToBoolean(this[this.CurrentCell]) == false) )
{
this[this.CurrentCell] = true;
}
else
{
this[this.CurrentCell] = false;
}
}
}
}
int m_lastRowIndex = 0;
/// <summary>
/// 原始的 LastRowIndex
/// </summary>
public int LastRowIndex
{
get
{
return this.m_lastRowIndex;
}
} }
<asp:BoundColumn DataField="XID" HeaderText="***">
</asp:BoundColumn>aspx.cs文件:
利用sql查询返回相应的字段
string strSql = "select xid from 表名"