使用c sharp在datagridview中增加S No [英] increment the S No in datagridview using c sharp

查看:89
本文介绍了使用c sharp在datagridview中增加S No的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意它是windows应用程序。



i有datagridview,其中没有列。



i想要在datagridview中增加S no。



代码如下;



Note it is windows application.

i have datagridview, in that s no column is there.

i want to increment the S no in the datagridview.

Code as follows;

int irow = 0;
sql = "Select  Sno  from Tb_Faculty_Availability";
dr = GFun.ReadAcessSql(sql);

while (dr.Read())
{
      DGVCalendar.Rows.Add();
      DGVCalendar[0, irow].Value = irow + 1;
}



当我运行上面的代码时,



在sno的datagridview列中是空的。



SNo



我希望输出如下;




when i run the above code,

in the datagridview column in sno is empty.

SNo

I want the output as follows;

Sno

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20



用于获取上面SNo的输出,在我的代码中需要进行哪些更改请帮帮我。



问候,

''Narasiman P



注意它是windows应用程序。


for getting output in the SNo above, in my code what changes required please help me.

Regards,
''Narasiman P

Note it is windows application.

推荐答案

使用datagridview DataBindingComplete事件并在此事件中编写此代码就像这样



Use datagridview DataBindingComplete event and write this code in this event just like this

private void DataGridView_DataBindingComplete(object sender, System.Windows.Forms.DataGridViewBindingCompleteEventArgs e)
        {
            int counter = 1;
            for (int i = 0; i < this.Rows.Count; i++)
            {
                this.Rows[i].Cells["Counter"].Value = (counter++).ToString();
            }
            this.Columns["Counter"].CellTemplate.Style.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;

            this.Columns["Counter"].Width = 60;
        }







希望这有帮助




Hope this helps


Dera Narasiman,



从sql获取所有数据到DataTable然后



Dera Narasiman,

Get all datas from sql to a DataTable then

for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvSample.Rows.Add();
                dgvSample[0, i].Value = i + 1;
                        //or
                dgvSample.Rows[i].Cells["sl_no"].Value = i + 1;
            }









问候

Mehaboob


这篇关于使用c sharp在datagridview中增加S No的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆