我想在DataGrid最前面的那个column中加入所得到的记录的序号,请问怎么做?
效果就像sql查询器得到查询数据后一样

解决方案 »

  1.   

    在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();
                }
            }
      

  2.   

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

  3.   

    楼上两位,谢谢阿,不过我忘记说了,我是windows应用程序。
      

  4.   

    参考这个,可以直接使用/// <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;
                }
            }    }
      

  5.   

    html:
    <asp:BoundColumn DataField="XID" HeaderText="***">
    </asp:BoundColumn>aspx.cs文件:
    利用sql查询返回相应的字段
    string strSql = "select xid from 表名"