Datable 詳解,及用法

2022-07-15 03:45:13 字數 1608 閱讀 7379

一、dataset、datatable、datarow、datacolumn

1】 在dataset中新增datatable

dataset.tables.add(datatable) 

例項:dataset ds=new dataset();

datatable table=new datatable("學生表");

ds.tables.add(table);

2】從dataset中讀出datatable

datatable=dataset.tables[0]或dataset.tables["表名"] 

例項:datatable table=ds[i]或datatable table=ds["學生表"]

//i為dataset中的索引值,因為dataset可存放多個datatable,可以根據索引值來找datatable或直接寫datatable的名稱來找datatable

3】新增行

datatable t=new datatable();

datarow r=t.newrow();

r["列名"]=列值;

t.rows.add(r) ;

例項:datatable:學生表

id    name

1    xun

**:datatable t=new datatable("學生表");

datarow r=t.newrow();

r["id"]=2;

r["name"]=xun2;

t.rows.add(r);

4】新增列

datatable.columns.add("列名",type.gettype("資料型別"))  ;

5】從行中讀列值

datarow["列名"]或datarow[datacolumn];  

6】從datatable中讀列值

datatable table;

a、table.rows[i]["列名"]

b、table.rows[i][i]

c、table[i].列名(列名不加引號)

7】讀出特定的行

datatable table;

datarow selectrow=table.select("列名='"+存放特定的變數.tostring()+"'");

選擇其中的一行:selectrow[索引]

二、刪除datatable中的行三種方法:(datatable.rows.remove(datarow dr)、datatable.rows.removeat(i)、datarow.delete())

刪除datatable中的行要注意索引問題,一般有兩種方法:

1】用for迴圈時,注意計數器初始值為表長,自減迴圈。datatable.rows.removeat(i)就要注意。

2】用datatable的select方法,注意該方法的引數是字串篩選器

3】delete()之後需要datatable.acceptechanges()方法確認完全刪除,因為delete()只是將相應列的狀態標誌為刪除,還可以通過datatable.rejectchanges()回滾,使該行取消刪除。若要刪除多行,可以連續用delete(),然後採用acceptechanges()方法確認刪除。

C Timer用法及例項詳解

c timer用法有哪些呢?我們在使用c timer時都會有自己的一些總結,那麼這裡向你介紹3種方法,希望對你了解和學習c timer使用的方法有所幫助。關於c timer類 在c 裡關於定時器類就有3個 c timer使用的方法1.定義在system.windows.forms裡 c timer使...

C Timer用法及例項詳解

c timer用法有哪些呢?我們在使用c timer時都會有自己的一些總結,那麼這裡向你介紹3種方法,希望對你了解和學習c timer使用的方法有所幫助。關於c timer類 在c 裡關於定時器類就有3個 c timer使用的方法1.定義在system.windows.forms裡 c timer使...

c timer用法及例項詳解

c timer類作為我們程式設計中的常用類,我們會遇到的c timer用法有哪些呢?具體的c timer使用過程是什麼呢?那麼本文就向你介紹具體的內容。c timer用法有哪些呢?我們在使用c timer時都會有自己的一些總結,那麼這裡向你介紹3種方法,希望對你了解和學習c timer使用的方法有所...