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" ];}

解决方案 »

  1.   

    我也是这样写的,为什么它还是在DATAGRID的最后面插ROW呢?
      

  2.   

    index应该是0。
    dataSet11.Tables["TEST1"].Rows.InsertAt(datarow1,0)
      

  3.   

    我都试了 很多次了,0没用,csdn高手们能帮我试试吗??
      

  4.   

    Inserts a new row into the collection at the specified location.[Visual Basic]
    <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.
      

  5.   

    stdao(道可道)提供的方法我也试过了,确实没问题。
    我想你们的不同之处在于,你的实际用法与其不同。
    我发现,在dataset作为datagrid的数据源之前使用InsertAt就能如你所愿;一旦绑定给了datagrid,即使用datagrid1.Datasource=null;这样的语句都不能完全断开他们的关系。也就是说这时候是按照datagrid的方式(排序)插入行的。因此,方法1:在插行之前刷新ds(New 或 Fill);方法2:datagrid按制定规则排序。
      

  6.   

    怎么样让datagrid按制定规则排序。???
      

  7.   

    怎样用dataview制定排序规则?????请指教??
      

  8.   

    因为ds是静态的数据集,插入数据后不可能调换其顺序,数据只能加在后面
    这并不是datagrid绑定的问题,datagrid绑定没有问题,即使前面没有绑定
    先在dataset中插入数据,最后进行绑定的话,插入数据还是在最后一行。