Question

I have read the definition from msdn (see below) which did not help me. I am dynamically adding gridviewrows to my gridview and don't know what are the parameters for.

public GridViewRow(
    int rowIndex,
    int dataItemIndex,
    DataControlRowType rowType,
    DataControlRowState rowState
)

rowIndex Type: System.Int32 The index of the GridViewRow object in the Rows collection of a GridView control.

dataItemIndex Type: System.Int32 The index of the DataItem in the underlying DataSet.

rowType Type: System.Web.UI.WebControls.DataControlRowType One of the DataControlRowType enumeration values.

rowState Type: System.Web.UI.WebControls.DataControlRowState A bitwise combination of the DataControlRowState enumeration values.

Was it helpful?

Solution

These parameters (and this constructor in general) really rarely need to be used. They are for creating a GridView, and it's rows, completely manually - something that's pretty unnecessary, considering the powerful databinding functionality that's built in to this control. Let me explain.

Generally, you should construct a datasource (DataTable, some generic list of a custom class, etc) and then assign that datasource to the GridView and bind it. This automates things like setting the RowIndex and DataItemIndex. It also allows for a number of other convenient features (sorting, paging, editing / deleting). See the GridView Web Server Control Overview for a great breakdown of this default functionality.

Thus, I would say that you should add the new row to your datasource (whatever that may be), then set the updated datasource to your GridView's DataSource property, and call GridView.DataBind(). You'll have your new row, and won't have to mess with creating GridViewRow objects manually.

But, to answer your question:

  • int rowIndex: The index (position) the row you're creating will occupy in the GridView.
  • int dataItemIndex: The index of the of this data in your underlying data source (the DataTable or generic list or whatever it is your using).
  • DataControlRowType rowType: This is the type of row - a row with data, a header, a footer, etc (full list here).
  • DataControlRowState rowState: The "state" the row is in - edit mode, read only mode, etc (full list here).

OTHER TIPS

  for example to add into gridview you can use datatable...

        public static DataTable TBCONRCVD;      

        FindRowNo = GoodsRcvdGridview.Items.Count;
        DataRow ROW = TBCONRCVD.NewRow();

        ROW["PRDCT_RCVD_PRDCT_CODE"] = TxtSearch.Text;
        ROW["PRDCT_RCVD_QTY"] = txtQty.Text.Trim();
        ROW["PRDCT_RCVD_COST"] = TXTUNITPRCE.Text.Trim();
        ROW["PRDCT_CRNT_SLNG_PRCE"] = SELL_PRCE.ToString();
        ROW["PRDCT_RCVD_VAT_CODE"] = TXTVATCODE.Text.Trim();
        ROW["PRDCT_RCVD_DISC"] = txtDscntPrcntge.Text;


        TBCONRCVD.Rows.Add(ROW);
        GoodsRcvdGridview.DataSource = TBCONRCVD;
        GoodsRcvdGridview.DataBind();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top