Saturday, July 19, 2008

How to Extract values from GridView Selected Row using DataKeys

To extract values from a Gridview selected row generally we use the following method.

Gridview1.Rows[Gridview1.SelectedIndex].cells[1].Text

The above method only works when the Gridview BoundField is in visible state. If we set the boundfield as visible="false", then the above code will fail to serve the purpose. To Extract values from invisible column of an Gridview we can use DataKeys propery. The following example shows how to use the DataKeys property to determine the data key value of the GridView selected row.

<asp:gridview id="JobsGridVeiw"
     datasourceid="JobsDataSource"
     autogeneratecolumns="false"
     datakeynames="JobID"
     onselectedindexchanged="JobsGridVeiw_SelectedIndexChanged"
    runat="server">
<Columns>
     <asp:boundfield datafield="JobID"
         readonly="true" visible="false"/>
     <asp:boundfield datafield="JobType"
        convertemptystringtonull="true" headertext="Job Type"/>
     <asp:boundfield datafield="JobNo"
        convertemptystringtonull="true" headertext="Job No"/>
     <asp:boundfield datafield="JobStatus"
        convertemptystringtonull="true" headertext="Job Status"/>
     <asp:boundfield datafield="JobDesc"
        convertemptystringtonull="true" headertext="Description"/>
</Columns>
</asp:gridview>

In the above example the BoundField JobID is set as visible="false". If you want to extract the value of JobID column you need to use the following code.

void JobsGridVeiw_SelectedIndexChanged(Object sender, EventArgs e)
{
    string JobID = JobsGridVeiw.DataKeys[JobsGridVeiw.SelectedIndex].Value.ToString();
}

Hope this post help u.

Tuesday, July 15, 2008

How to Customize GridView Pager at Runtime Using ObjectDataSource

To display bulk amounts of data in a GridView, it's often better to use Paging concept. When creating a pageable GridView whose underlying data is coming from an ObjectDataSource, you'll need to ensure that your Business Logic Layer includes methods that have input parameters that specify how to create the Paging Details. The ObjectDataSource supports both the default and custom paging models.

Inorder to support custom paging, the Business Logic Layer must include some additional features. The underlying Buniness Logoc Layer class's SELECT method must accept 2 integer arguments, the first one specifying the maximum number of records to return and the second specifying the starting record index. To know how to do all this please visit the following website

http://msdn.microsoft.com/en-us/library/aa479347.aspx

Hope this help you a lot.

Sunday, July 13, 2008

Create Rows Dynamically to a GridView

Many times we worried how to add rows dynamically to a GridView. This might be the case when we need to display the Master - Detail Information. For Example, There is a GridView populated with the Department data. Here our requirment is whenever we select a department from the GrdiView, automatically a new row need to be created exactly next to the selected row which contains another GridView populated with Employees data related to select Department. If this is the case first we need to create a new exactly next to the selected row. These are the steps to create a new row :

  1. Add a GridView to your aspx page.

  2. Populate with data using any datasource control (ObjectDataSource, SQLDataSource, etc.)


Then, your ccode may looks like:


< asp:GridView ID="departmentGridView" Runat="server" DataSourceID="DepartmentObjectDataSource" DataKeyNames="DeptID" AutoGenerateColumns="False" cellpadding="5" OnSelectedIndexChanged="departmentGridView_SelectedIndexChanged" >
     .......
     .......
     .......
</asp:GridView>

In the above code you find a event OnSelectedIndexChanged which fires when a row's Select button is clicked, but after the GridView control handles the select operation. Now, we use this onSelectedIndexChanged Event to create a new row exactly next to the selected row. Then the code for OnSelectedIndexChanged event may looks like :


protected void departmentGridView_SelectedIndexChanged(object sender, EventArgs e)
{
     if (departmentGridView.SelectedRow != null)
     {
          Table table = new Table();
          if (table != null)
                addNewRow(table, departmentGridView.SelectedIndex)
     }
}

private void addNewRow(Table table, int SelectedIndex)
{
     GridViewRow GridRow = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
     Row.Cells.Add(generateColumns());
     table.Rows.AddAt(SelectedIndex+ 2, GridRow);
}

private TableCell generateColumns()
{
     TableCell Cell1= new TableCell();
     Cell1.ColumnSpan = departmentGridView.Columns.Count;
     Cell1.style["width"] = "100%";
     /* Here you can create any control and add that control to this Cell */
     Button btn = new Button();
     btn.Text = "Click Me";
     Cell1.Controls.Add(btn);
     return Cell1;
}

In the above code instead of Button control you can create a GridView Control and add it to the TableCell. By this way we can dynamically add row to a GridView on demand.