我有一個數據網格視圖,我把它DataSource屬性
作爲如何顯示序列號。在C#窗口的數據網格視圖形成
dgvEmployee.DataSource = mycollection;
我想,電網應該有一個像
dgvEmployee.ShowSerialNumber = True;
任何建議某些屬性
在此先感謝
我有一個數據網格視圖,我把它DataSource屬性
作爲如何顯示序列號。在C#窗口的數據網格視圖形成
dgvEmployee.DataSource = mycollection;
我想,電網應該有一個像
dgvEmployee.ShowSerialNumber = True;
任何建議某些屬性
在此先感謝
這段代碼是從here
/// This class extends the the DataGridView so row numbers will
/// automatically appear in the row header cells. In this
/// implementation, the width of the column that contains the row
/// header cells is automatically adjusted to accomodate the row
/// numbering.
/// ******************************************************************
/// AUTHOR: Daniel S. Soper
/// URL: http://www.danielsoper.com
/// DATE: 20 February 2007
/// LICENSE: Public Domain. Enjoy! :-)
/// ******************************************************************
///
class MyDGV : DataGridView
{
public MyDGV()
{
//perform any necessary customization initialization here
} //end default constructor
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{ //this method overrides the DataGridView's RowPostPaint event
//in order to automatically draw numbers on the row header cells
//and to automatically adjust the width of the column containing
//the row header cells so that it can accommodate the new row
//numbers,
//store a string representation of the row number in 'strRowNumber'
string strRowNumber = (e.RowIndex + 1).ToString();
//prepend leading zeros to the string if necessary to improve
//appearance. For example, if there are ten rows in the grid,
//row seven will be numbered as "07" instead of "7". Similarly, if
//there are 100 rows in the grid, row seven will be numbered as "007".
while (strRowNumber.Length < this.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;
//determine the display size of the row number string using
//the DataGridView's current font.
SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
//adjust the width of the column that contains the row header cells
//if necessary
if (this.RowHeadersWidth < (int)(size.Width + 20)) this.RowHeadersWidth = (int)(size.Width + 20);
//this brush will be used to draw the row number string on the
//row header cell using the system's current ControlText color
Brush b = SystemBrushes.ControlText;
//draw the row number string on the current row header cell using
//the brush defined above and the DataGridView's default font
e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height)/2));
//call the base object's OnRowPostPaint method
base.OnRowPostPaint(e);
} //end OnRowPostPaint method
} //end class
在數據網格中添加新的模板字段,
模板字段中添加一個標籤..
在datgrid的RowDataBound事件設置標籤文本的序列號。 http://bytes.com/topic/c-sharp/answers/874915-how-generate-serial-number-datagrid-view-automatically
子網格並添加它。
class MyGrid : DataGridView
{
public bool ShowSerialNumber
{
get ...
set ...
}
}
採取試試這個......我不知道,但嘗試。
填補數據庫數據表,並添加新列的DataTable
DataColumn dcAuto = new DataColumn();
dcAuto.AutoIncrement = true;
dcAuto.AutoIncrementSeed = 1;
dt.Columns.Add(dcAuto);
將DataTable綁定到GridView。
//1-add new column to dataGridView1 call it ser or your Name
//2-set it's DataPropertyName To None "Very Important"
//3-Set HeaderText to ("Your Name To shown In Header")
//4-Set frozen To True
//5-in DefultCellStyle Do Thise
// A-Set BackColor To AnyColor you Want
// b-Set Null Value to 0 or 1 as you prefer
// b-Set Alignment To middleCenter
//6- call Method MakeSerieal() everyTime You Want To fill dataGridView1
//7- call Method MakeSerieal() In ColumnHeaderMouseClick ("It help if you Want To Reorder dataGridView1")
//--------------------------
private void MakeSerieal()
{
if (dataGridView1.Rows.Count > 1)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
dataGridView1.Rows[i].Cells[0].Value = (i + 1).ToString();
}
}
}
//--------------------------
//With My Regards
//Ahmed Mansour ("Egypt")
//[email protected]>com
簡單,短期和正確答案+1的內置特性 – 2016-06-23 21:40:37
嘗試這些肯定工作:
SqlDataAdapter sda0 = new SqlDataAdapter(sqlCmd0);
DataTable Dt0 = new DataTable();
Dt0.Columns.Add("SlNo");
Dt0.Columns["SlNo"].AutoIncrement = true;
Dt0.Columns["SlNo"].AutoIncrementSeed = 1;
sda0.Fill(Dt0);
grdName.DataSource = Dt0;
這有點像人需要序列號添加到他們的datgridview和你想要的序列號,以像在Microsoft Word中項目符號和編號規定: - 這裏是答案。您可以撥打datagridview的特性這種方法「RowsAdded」和「RowsRemoved」
//improvised answer of ahmed mansour
private void MakeSerieal()
{
if (dataGridView1.Rows.Count > 0)
{
int slno = 0;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
slno= slno+1;
dataGridView1.Rows[i].Cells[0].Value = slno.ToString();
}
}
}
我想,它應該工作,因爲它是datagridview的 – 2012-01-02 07:40:49