Få fram antal rader i en GridView
En kort beskrivning hur man kan få fram antal raden som visas i en GridView, som är kopplat till en SqlDataSource, genom OnSelected event. Och då snackar vi som vanligt .NET. Koden för GridView och SqlDataSource:
<asp:Label id="lblProductTitle"><asp:GridView ID="gvProduct" EnableViewState="true"
DataSourceID="sdsProduct"
DataKeyNames="Id"
AllowPaging="true"
AllowSorting="true"
AutoGenerateColumns="false"
runat="server">
<Columns>
<asp:TemplateField HeaderText="Bild">
<ItemTemplate>
<asp:PlaceHolder ID="phThumb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Artnr" DataField="Id" ItemStyle-Wrap="false" SortExpression="Id" />
<asp:BoundField HeaderText="Produktnamn" DataField="ProductName" ItemStyle-Wrap="false" SortExpression="ProductName" />
<asp:BoundField HeaderText="Märke" DataField="Brand" ItemStyle-Wrap="false" SortExpression="Brand" />
<asp:BoundField HeaderText="Kategori" DataField="Category" ItemStyle-Wrap="false" SortExpression="Category" />
<asp:BoundField HeaderText="Pris" DataField="Price" ItemStyle-Wrap="false" SortExpression="Price" DataFormatString="{0:c0} kr" />
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="sdsProduct"
runat="server"
EnableViewState="true"
SelectCommand="SELECT * FROM Product"
OnSelected="sdsProduct_Selected"
ConnectionString="<%$ appSettings:ConnectionString %>"
DataSourceMode="DataSet">
</asp:SqlDataSource>
DataSourceID="sdsProduct"
DataKeyNames="Id"
AllowPaging="true"
AllowSorting="true"
AutoGenerateColumns="false"
runat="server">
<Columns>
<asp:TemplateField HeaderText="Bild">
<ItemTemplate>
<asp:PlaceHolder ID="phThumb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Artnr" DataField="Id" ItemStyle-Wrap="false" SortExpression="Id" />
<asp:BoundField HeaderText="Produktnamn" DataField="ProductName" ItemStyle-Wrap="false" SortExpression="ProductName" />
<asp:BoundField HeaderText="Märke" DataField="Brand" ItemStyle-Wrap="false" SortExpression="Brand" />
<asp:BoundField HeaderText="Kategori" DataField="Category" ItemStyle-Wrap="false" SortExpression="Category" />
<asp:BoundField HeaderText="Pris" DataField="Price" ItemStyle-Wrap="false" SortExpression="Price" DataFormatString="{0:c0} kr" />
</Columns>
</asp:GridView>
<asp:SqlDataSource
ID="sdsProduct"
runat="server"
EnableViewState="true"
SelectCommand="SELECT * FROM Product"
OnSelected="sdsProduct_Selected"
ConnectionString="<%$ appSettings:ConnectionString %>"
DataSourceMode="DataSet">
</asp:SqlDataSource>
Och koden bakom som skriver ut antalet rader i gridden:
protected void sdsProduct_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
int intTotalRecord = Convert.ToInt32(e.AffectedRows.ToString());
if (intTotalRecord > 0)
{
lblProductTitle.Text = intTotalRecord.ToString() + " produkter hittades";
}
else
{
lblProductTitle.Text = "Inga produkter hittades";
}
Trace.Write("intTotalRecord = " + intTotalRecord);
}
{
int intTotalRecord = Convert.ToInt32(e.AffectedRows.ToString());
if (intTotalRecord > 0)
{
lblProductTitle.Text = intTotalRecord.ToString() + " produkter hittades";
}
else
{
lblProductTitle.Text = "Inga produkter hittades";
}
Trace.Write("intTotalRecord = " + intTotalRecord);
}
Tags