1

I have a stored procedure like this:

ALTER procedure [dbo].[fetchkey]
@carid nvarchar(50) =null
as
begin
 select t.TBarcode, t.Status,[dbo].[keyloc](t.status,@carid) as 'keylocation'
 from Transaction_tbl t 
 where t.TBarcode=@carid
end

Also i have function like this:

ALTER function [dbo].[keyloc](@status numeric(18,2),@cardID VARCHAR(50)) RETURNS varchar(50)
as 
begin 
  @Ename nvarchar(50)
 , @keylocation  Varchar(50)

 if @status=0 
begin 
 select @keylocation= 0 
end
 return @keylocation
end

while executing my stored procedure i am getting out put like this:

TBarcode             Status      keylocation
53012364813          0             0

am filling this data to directly data grid view ,here my vb.net code

Dim cmd As New SqlCommand("fetchkey", con.connect) 
cmd.CommandType = CommandType.StoredProcedure 
cmd.Parameters.Add("@carid", SqlDbType.Int).Value = carid     
da.SelectCommand = cmd
da.Fill(ds)
DGVDashBoard.DataSource = ds.Tables(0).    

if i bind like this,in data grideviw i am getting 3 column.in data grid view i want to display only Tbarcode value and status value.also i want to show the particular data grid view row in red color becouse of the keylocation 0.how i can achieve this.if any one know how to do,please help me.

Riv
  • 1,849
  • 1
  • 16
  • 16
user2603688
  • 171
  • 3
  • 5
  • 11
  • Concerning the columns you want/dont want to display, Take a look at this answer http://stackoverflow.com/a/16744811/2387010 – Chris Jul 25 '13 at 10:41

1 Answers1

1

Without seeing the html of the grid view I can only offer advice.

If you want to use just some columns from a dataset and have conditional formatting, then you need to do some processing in the Girdview's RowDataBound event.

I would suggest this:

    Protected Sub YourGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles YourGridView.RowDataBound
        Dim row As GridViewRow = e.Row
        Dim currentRow As System.Data.DataRow = CType(row.DataItem, System.Data.DataRow)

        If row.RowType = DataControlRowType.DataRow Then
            If currentRow("keylocation") = 0 Then
                row.Style.Add("background", "red")
                row.Style.Add("color", "white")
            End If

            CType(row.FindControl("TBarcodeLabel"), Label).Text = currentRow("TBarcode")
            CType(row.FindControl("StatusLabel"), Label).Text = currentRow("Status")
        End If
    End Sub

This code assumes you have a grid view with two Label controls added somewhere in it's row template that can be populated.

It would look something like this:

<asp:GridView ID="YourGridView" runat="server">
    <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="50">
        <ItemTemplate>
            <asp:Label ID="TBarcodeLabel" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="50">
        <ItemTemplate>
            <asp:Label ID="StatusLabel" runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
</asp:GridView>

In this way you are handling the population of the rows and thus can do whatever formatting to them you want, including dropping columns.

Pow-Ian
  • 3,607
  • 1
  • 22
  • 31