Introduction:
Selecting a row means that the user can highlight or change the appearance of a row by clicking some sort of button or link. When the user clicks the button, not only will the row change its appearance, but also your code will have the opportunity to handle the event.
Main:
The GridView provides built-in support for selection,
You just need to add CommandField for selecting particular row
<asp:CommandField ShowSelectButton=”True” ButtonType=”Button”
SelectText=”Select” />
You can easily retrieve select rows details in the following
two easy eays,
1.SelectedIndexChanged Property,
2.using SqlDataSource
1.SelectedIndexChanged Property
Here you have one problem,sometimes SelectedIndex returns a zero-based index number representing where the row occurs in the grid
See this belo examble,
protected void gridEmployees_SelectedIndexChanged(object sender, EventArgs e)
{
int index = gridEmployees.SelectedIndex;
// You can retrieve the key field from the SelectedDataKey property.
int ID = (int)gridEmployees.SelectedDataKey.Values["EmployeeID"];
// You can retrieve other data directly from the Cells collection,
// as long as you know the column offset.
string firstName = gridEmployees.SelectedRow.Cells[2].Text;
string lastName = gridEmployees.SelectedRow.Cells[3].Text;
lblRegionCaption.Text = “Regions that ” + firstName + ” ” + lastName +
” (employee ” + ID.ToString() + “) is responsible for:”;
}
This above code will give the details of particular row in gridview(If it’s selected)
2.Using SqlDataSource
Datagrid Decalration,
<asp:GridView ID=”gridEmployees” runat=”server” DataSourceID=”sourceEmployees”
DataKeyNames=”EmployeeID” … >
If we select any row from gridEmployees it will display in next grid gridRegions,
<asp:GridView ID=”gridRegions” runat=”server” DataSourceID=”sourceRegions” …>
…
<Columns>
<asp:BoundField DataField=”TerritoryID” HeaderText=”ID” />
<asp:BoundField DataField=”TerritoryDescription”
HeaderText=”Description”/>
</Columns>
</asp:GridView>
Please check this below code,
<asp:SqlDataSource ID=”sourceRegions” runat=”server”
ConnectionString=”<%$ ConnectionStrings:Northwind %>”
ProviderName=”System.Data.SqlClient” SelectCommand=”SELECT Employees.EmployeeID,
Territories.TerritoryID, Territories.TerritoryDescription FROM Employees INNER JOIN
EmployeeTerritories ON Employees.EmployeeID = EmployeeTerritories.EmployeeID
INNER JOIN Territories ON EmployeeTerritories.TerritoryID = Territories.TerritoryID
WHERE (Employees.EmployeeID = @EmployeeID)” >
<SelectParameters>
<asp:ControlParameter ControlID=”gridEmployees” Name=”EmployeeID”
PropertyName=”SelectedDataKey.Values["EmployeeID"]” />
</SelectParameters>
</asp:SqlDataSource>
Conclusion:
Now,we easily retrieved selected row’s details.
Hope,this helps.
I think this is a great post. One thing that I find the most helpful is number five. Sometimes when I write, I just let the flow of the words and information come out so much that I loose the purpose. It’s only after editing when I realize what I’ve done. There’s defiantly a lot of great tips here I’m going to try to be more aware of.