In this example i'm going to explain how to Conditionally Hide Or Disable GridView CommandField Or ButtonField Programmatically In Asp.Net Using C# And VB
I'll use field such as Select,ShowEditButton and controls in TemplateField for this demo.
Northwind Databse is used to populate GridView.
Enable selection from smart tag in design mode to show SelectButton Hyperlink in Gridview.
This Linkbutton will be hidden where country name is equal to Mexico and disabled where name is Germany in any row of grid as shown above in image.
Checkbox Control placed in ItemTemplate will also be covered.
HTML SOURCE OF GRIDVIEW
C# CODE
VB.NET CODE
I'll use field such as Select,ShowEditButton and controls in TemplateField for this demo.
Northwind Databse is used to populate GridView.
Enable selection from smart tag in design mode to show SelectButton Hyperlink in Gridview.
This Linkbutton will be hidden where country name is equal to Mexico and disabled where name is Germany in any row of grid as shown above in image.
Checkbox Control placed in ItemTemplate will also be covered.
HTML SOURCE OF GRIDVIEW
1: <asp:GridView ID="GridView1" runat="server"
2: DataSourceID="sqlDataSourceGridView"
3: onrowdatabound="GridView1_RowDataBound"
4: AutoGenerateColumns="false">
5: <Columns>
6: <asp:CommandField ShowSelectButton="True"/>
7:
8: <asp:TemplateField>
9: <ItemTemplate>
10: <asp:CheckBox ID="chkSelect" runat="server"
11: Visible='<%# ShowHide(Eval("Country"))%>'/>
12: </ItemTemplate>
13: </asp:TemplateField>
14: <asp:BoundField DataField="CustomerID"
15: HeaderText="Customer ID"/>
16: <asp:BoundField DataField="City" HeaderText="city"/>
17: <asp:BoundField DataField="Country"
18: HeaderText="Country"/>
19: </Columns>
20: </asp:GridView>
21:
22: <asp:SqlDataSource ID="sqlDataSourceGridView"
23: runat="server"
24: ConnectionString=
25: "<%$ ConnectionStrings:northWindConnectionString %>"
26: SelectCommand="SELECT [CustomerID], [City], [Country]
27: FROM [Customers]">
28: </asp:SqlDataSource>
C# CODE
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string country = DataBinder.Eval(e.Row.DataItem, "Country").ToString(); if (country == "Mexico") { LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[0]; lb.Visible = false; } else if (country == "Germany") { e.Row.Cells[0].Enabled = false; } } } protected bool ShowHide(object country) { if (country.ToString() == "France") return false; else return true; }
VB.NET CODE
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim country As String = DataBinder.Eval(e.Row.DataItem, "Country").ToString() If country = "Mexico" Then Dim lb As LinkButton = DirectCast(e.Row.Cells(0).Controls(0), LinkButton) lb.Visible = False ElseIf country = "Germany" Then e.Row.Cells(0).Enabled = False End If End If End Sub Protected Function ShowHide(country As Object) As Boolean If country.ToString() = "France" Then Return False Else Return True End If End Function
If you like this post than join us or share
2 comments:
Nicely done.!! :)
Thank you very much, It helped me a lot,
Appreciated your help.
-Nag
Post a Comment