protected void dg_bind() {
System.Data.SqlClient.SqlConnection SqlCon = new System.Data.SqlClient.SqlConnection( "server=localhost;database=northwind;trusted_connection=true;" );
System.Data.SqlClient.SqlDataAdapter SqlDa = new System.Data.SqlClient.SqlDataAdapter( "SELECT TOP 10 ProductName FROM Products", SqlCon );
ds = new System.Data.DataSet( );
SqlDa.Fill( ds, "Products" );
System.Data.DataRow BlankRow = ds.Tables[ "Products" ].NewRow( );
BlankRow[ "ProductName" ] = "&alt;font color=\"red\"&agt;add new product&alt;/font&agt;";
ds.Tables[ "Products" ].Rows.InsertAt( BlankRow, 0 );
dg.DataSource = ds.Tables[ "Products" ];}
System.Data.SqlClient.SqlConnection SqlCon = new System.Data.SqlClient.SqlConnection( "server=localhost;database=northwind;trusted_connection=true;" );
System.Data.SqlClient.SqlDataAdapter SqlDa = new System.Data.SqlClient.SqlDataAdapter( "SELECT TOP 10 ProductName FROM Products", SqlCon );
ds = new System.Data.DataSet( );
SqlDa.Fill( ds, "Products" );
System.Data.DataRow BlankRow = ds.Tables[ "Products" ].NewRow( );
BlankRow[ "ProductName" ] = "&alt;font color=\"red\"&agt;add new product&alt;/font&agt;";
ds.Tables[ "Products" ].Rows.InsertAt( BlankRow, 0 );
dg.DataSource = ds.Tables[ "Products" ];}
dataSet11.Tables["TEST1"].Rows.InsertAt(datarow1,0)
<Serializable>
Public Sub InsertAt( _
ByVal row As DataRow, _
ByVal pos As Integer _
)
[C#]
[Serializable]
public void InsertAt(
DataRow row,
int pos
);
[C++]
[Serializable]
public: void InsertAt(
DataRow* row,
int pos
);
[JScript]
public
Serializable
function InsertAt(
row : DataRow,
pos : int
);
Parameters
row
The DataRow to add.
pos
The location in the collection where you want to add the DataRow.
Res
If the value specified for the pos parameter is greater than the number of rows in the collection, the new row is added to the end.
我想你们的不同之处在于,你的实际用法与其不同。
我发现,在dataset作为datagrid的数据源之前使用InsertAt就能如你所愿;一旦绑定给了datagrid,即使用datagrid1.Datasource=null;这样的语句都不能完全断开他们的关系。也就是说这时候是按照datagrid的方式(排序)插入行的。因此,方法1:在插行之前刷新ds(New 或 Fill);方法2:datagrid按制定规则排序。
这并不是datagrid绑定的问题,datagrid绑定没有问题,即使前面没有绑定
先在dataset中插入数据,最后进行绑定的话,插入数据还是在最后一行。