Jay_Vora_b4843e

hi all.

i hv a datgrid in which the frst column is of serial no.

can anyone tell me how to specify for tht column n increment it evry time new records get inserted..

like..

datagrid:

serial no. user date address

1 y xxx xxx

2

3

n so n so..

so whts for serail no

thx .




Re: Windows Forms Data Controls and Databinding serial no problem in datagrid..

Hayder Marzouk

Hi,

There no property in the grid to do this.

This feature must be implemented in ure datasource (Collection or dataset).

Specify ure datasource type so i can help you.

HTH

Hayder Marzouk





Re: Windows Forms Data Controls and Databinding serial no problem in datagrid..

VictorC

You cannot do this directly to the datagrid [since a datagrid doesn't store information it just displays it] so you have to do it to the underlying datacolumn.
Just set the AutoIncrement attribute to true and play around with AutoIncrementSeed and AutoIncrementStep depending on what you need [from what you said 1, 1 seems right]

Heres an example:

Code Snippet

DataTable dt = new DataTable("TheTable");

DataColumn dc = new DataColumn ("SerialNo", typeof(int));

dc.AutoIncrement = true;

dc.AutoIncrementSeed = 1;

dc.AutoIncrementStep = 1;

dt.Columns.Add(dc);





Re: Windows Forms Data Controls and Databinding serial no problem in datagrid..

Jay Vora

see i hv a list of items..

like

for i=0 to 5

dim items(4) as object

items(0)=i+1 // here i want serail no.

items(1)=ds.tables(0).rows(i).item(0);

n so to 5

dt.rows.add(items)

next

help for items(0) ..

evry time i hv to add the code snippet wht for my code victor wll ur code work for me i hv tried this style code from msdn before..

thx evryone






Re: Windows Forms Data Controls and Databinding serial no problem in datagrid..

VictorC

I don't know VB syntax so bare with me and I'll illustrate in C#:

Heres how I would do what you're asking for:

1] Create the dataset/datatable and add your datacolumns to it. Set your serialNo datacolumn to autoincrement:

Code Snippet

DataSet ds1 = new DataSet();
DataTable dt1 = new DataTable();
DataColumn dc1 = new DataColumn("serialNo");
dc1.AutoIncrement = true;
dc1.AutoIncrementSeed = 1;
dc1.AutoIncrementStep = 1;
dt1.Columns.Add(dc1);



2] Create the rest of your columns:

Code Snippet

dt1.Columns.Add("name");
dt1.Columns.Add("age");
dt1.Columns.Add("gender");



3] Create your items object array to pass as a datarow and be sure to keep the value corresponding to the serialNo column as null:

Code Snippet

object[] items = new object[4];
items[0] = null;
items[1] = "bob";
items[2] = 42;
items[3] = "M";

dt1.Rows.Add(items);



This should work [once you switch it over to VB syntax]




Re: Windows Forms Data Controls and Databinding serial no problem in datagrid..

Gavin Jin - MSFT

Hi,Jay

Here is another method,maybe useful to you.It can add NO. in rowheaders,hope it helps.

Private Sub dataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As DataGridViewRowPostPaintEventArgs) Handles dataGridView1.RowPostPaint

' Paint the row number on the row header.

' The using statement automatically disposes the brush.

Using b As SolidBrush = New SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)

e.Graphics.DrawString(e.RowIndex.ToString(System.Globalization.CultureInfo.CurrentUICulture), _

dataGridView1.DefaultCellStyle.Font, _

b, _

e.RowBounds.Location.X + 20, _

e.RowBounds.Location.Y + 4)

End Using

End Sub

Regards.






Re: Windows Forms Data Controls and Databinding serial no problem in datagrid..

Jay Vora

hey..thx all for ur warm help..

i got tht solved using..

items(0) = i + 1

earlier i used one dataset only..so was the problem..

now i use more than one datasets..

so i got it done..

overview ::

dt.Columns.Add("serial", GetType(Integer))

For i = 0 To ds.Tables(0).Rows.Count - 1

Dim items(5) As Object

items(0) = i + 1

Dim ds3 As DataSet

sql = "select [FName],[LName] from [ClientMaster] where [ClientNo] = " + CStr(ds.Tables(0).Rows(i).Item(0))

ds3 = DbControl.GetDataSet(sql)

items(1) = CStr(ds3.Tables(0).Rows(0).Item(0)) + " " + CStr(ds3.Tables(0).Rows(0).Item(1))

' Response.Write(j)

'Dim tdate As String

' tdate = CDate(ds.Tables(0).Rows(i).Item(0)).ToLongDateString

items(2) = (ds.Tables(0).Rows(i).Item(1))

'Dim a As String

Dim ds2 As DataSet = New DataSet

sql = "select [pname],[catname] from [productmaster],[categorymaster] where [productmaster].[pcatid] = [categorymaster].[catid] and [productmaster].[pid]=" + CStr(ds.Tables(0).Rows(i).Item(2))

'a = ds.Tables(0).Rows(i).Item(1)

ds2 = DbControl.GetDataSet(sql)

items(3) = CStr(ds2.Tables(0).Rows(0).Item(0)) + "-" + CStr(ds2.Tables(0).Rows(0).Item(1))

'Dim ds1 As New DataSet

'Dim sql1 As String

'sql1 = "Select [PDesc] from [ProductMaster] where [pid]=" & a.ToString

'ds1 = DbControl.GetDataSet(sql1)

'items(2) = ds.Tables(0).Rows(i).Item(1)

items(4) = ds.Tables(0).Rows(i).Item(3)

items(5) = ds.Tables(0).Rows(i).Item(4)

'items()

dt.Rows.Add(items)

dt.AcceptChanges()

'Response.Write(a)

Next

'dt.AcceptChanges()

DataGrid1.DataSource = ds1

DataGrid1.DataBind()