Gridview ASP.NET

Connect gridview to Generic List, Array or DataTable

<asp:GridView ID="gvFeeRules" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:CommandField ShowSelectButton="true" />
<asp:BoundField DataField="name" HeaderText="Rule" ReadOnly="True" SortExpression="name" />
<asp:BoundField DataField="startDate" HeaderText="Start date" SortExpression="startDate" />
<asp:BoundField DataField="endDate" HeaderText="End date" SortExpression="endDate" />
<asp:CommandField ShowEditButton="true" />
</Columns>
</asp:GridView>

Set width textbox in edit-mode: ControlStyle-Width="800px"
gvFeeRules.DataSource = mFee.mRules;
gvFeeRules.DataBind();

Mind the { get; set; } in the business layer!

Have an invisible id in gridview

Use DataKeyNames:

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="False" DataKeyNames="id">

id has to be in the datasource the gridview is linked to.
Result: grid.SelectedDataKey.Value

Events

When the gridview is not connected to a database, you need to provide this events

using System.Web.UI.WebControls;

protected void TaskGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    TaskGridView.PageIndex = e.NewPageIndex;
    //Bind data to the GridView control.
    BindData();
  }

  protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
  {
    //Set the edit index.
    TaskGridView.EditIndex = e.NewEditIndex;
    //Bind data to the GridView control.
    BindData();
  }

  protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  {
    //Reset the edit index.
    TaskGridView.EditIndex = -1;
    //Bind data to the GridView control.
    BindData();
  }

  protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {    
    //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session["TaskTable"];

    //Update the values.
    GridViewRow row = TaskGridView.Rows[e.RowIndex];
    dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;  // Mind: read-only columns don't have a Controls collection. You have to take Cells[x].Text in that case.

    //Reset the edit index.
    TaskGridView.EditIndex = -1;

    //Bind data to the GridView control.
    BindData();
  }

optional:
protected void TaskGridView_SelectedIndexChanged(object sender, EventArgs e)
{
  TaskGridView.SelectedRow.DataItemIndex
}

Insert

The gridview doesn't support inserting new records. There is a trick with the footer, but it's better to have a insert panel.
You can add an insert button in the gridview with this commandfield:
<asp:CommandField ShowInsertButton="true" />
You can catch the click with:
protected void gvRbm_RowCommand(object sender, GridViewCommandEventArgs e)
if (e.CommandName == "New")

Connect gridview to businesslayer

Don't use this anymore: code in asp page, not supported by Intellisense (Find All References)

<asp:GridView ID="grdEmail" runat="server" AutoGenerateColumns="False" DataSourceID="odsEmail" OnRowCommand ="grdEmail_OnRowCommand">
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="RowEdit" Text="Edit" />
<asp:BoundField DataField="settingsID" HeaderText="ID" SortExpression="mSettingsID" />
<asp:BoundField DataField="adress" HeaderText="To" SortExpression="mTo" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="odsEmail" runat="server" 
DataObjectTypeName="BE.EmailSettings" SelectMethod="GetEmailSettings" TypeName="BL.EmailManager">
</asp:ObjectDataSource>

Parameters:

this.odsEmail.SelectMethod = "GetCountryParameters";
this.odsEmail.SelectParameters.Add("countryId", System.Data.DbType.Int32, MasterPage.countryID.ToString());

Mind the { get; set; } in the business layer!
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License