Increment a Rowcounter in ADO.NET 2.0

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have an integer rowcounter column in a Table and I want to incremented it
after AddRow. AutoInc is not possible, because I want to change it later.
RowChanged doesn't seem to be the right place. Is there any OnAfterInsert
like in Delphi?

Thanks
Friedhelm Drecktrah
 
Hi,

Why is RowChanged the right place and why do you need this value?
This works fine:
static void Main(string[] args)

{

DataTable table = new DataTable();

table.Columns.Add("Tubo", typeof(int));

table.Columns.Add("Auto", typeof(int));

table.RowChanged += new DataRowChangeEventHandler(table_RowChanged);

DataRow row = table.NewRow();

row["Tubo"] = 1;

table.Rows.Add(row);

Console.WriteLine(row["Auto"]);

Console.Read();

}

private static void table_RowChanged(object sender, DataRowChangeEventArgs
e)

{

e.Row["Auto"] = e.Row.Table.Rows.Count+1;

}
 
Are you sure AutoInc is not possible? Althought autoincrement columns are
almost always readonly, autoincrement columns can be writable. Please try
the following code:

Sub Test()
Dim table As New DataTable("SomeTable")
Dim CounterColumn As DataColumn = table.Columns.Add("Counter",
GetType(Integer))
CounterColumn.AutoIncrement = True
CounterColumn.AutoIncrementSeed = 1
CounterColumn.AutoIncrementStep = 1
CounterColumn.ReadOnly = False
table.Columns.Add("Text", GetType(String))

For i As Integer = 0 To 20
Dim row As DataRow = table.NewRow()
row("Text") = "Text " & i.ToString()
table.Rows.Add(row)
row.AcceptChanges()
Next

For i As Integer = 20 To 0 Step -1
table.Rows(i).Item("Counter") = i * 2
Next
End Sub

Regards from Madrid (Spain)
Jesús López
VB MVP






"Friedhelm Drecktrah" <[email protected]>
escribió en el mensaje
news:[email protected]...
 
That's cool, thanks!

Miha Markic said:
Hi,

Why is RowChanged the right place and why do you need this value?
This works fine:
static void Main(string[] args)

{

DataTable table = new DataTable();

table.Columns.Add("Tubo", typeof(int));

table.Columns.Add("Auto", typeof(int));

table.RowChanged += new DataRowChangeEventHandler(table_RowChanged);

DataRow row = table.NewRow();

row["Tubo"] = 1;

table.Rows.Add(row);

Console.WriteLine(row["Auto"]);

Console.Read();

}

private static void table_RowChanged(object sender, DataRowChangeEventArgs
e)

{

e.Row["Auto"] = e.Row.Table.Rows.Count+1;

}
--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/


Friedhelm Drecktrah said:
Hi,
I have an integer rowcounter column in a Table and I want to incremented
it
after AddRow. AutoInc is not possible, because I want to change it later.
RowChanged doesn't seem to be the right place. Is there any OnAfterInsert
like in Delphi?

Thanks
Friedhelm Drecktrah
 
Back
Top