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 asSelect,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 SelectButtonHyperlink in Gridview.
This Linkbutton will be hiddenwhere country name is equal toMexico and disabled where name is Germany in any row of grid as shown above in image.
Checkbox Control placed inItemTemplate will also be covered.
HTML SOURCE OF GRIDVIEW
C# CODE
VB.NET CODE
I'll use field such asSelect,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 SelectButtonHyperlink in Gridview.
This Linkbutton will be hiddenwhere country name is equal toMexico and disabled where name is Germany in any row of grid as shown above in image.
Checkbox Control placed inItemTemplate 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
01
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
02
{
03
if
(e.Row.RowType == DataControlRowType.DataRow)
04
{
05
string
country = DataBinder.Eval(e.Row.DataItem,
"Country"
).ToString();
06
if
(country ==
"Mexico"
)
07
{
08
09
LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[0];
10
lb.Visible =
false
;
11
}
12
else
if
(country ==
"Germany"
)
13
{
14
e.Row.Cells[0].Enabled =
false
;
15
}
16
17
}
18
}
19
20
protected
bool
ShowHide(
object
country)
21
{
22
if
(country.ToString() ==
"France"
)
23
return
false
;
24
else
25
return
true
;
26
}
VB.NET CODE
01
Protected
Sub
GridView1_RowDataBound(sender
As
Object
, e
As
GridViewRowEventArgs)
02
If
e.Row.RowType = DataControlRowType.DataRow
Then
03
Dim
country
As
String
= DataBinder.Eval(e.Row.DataItem,
"Country"
).ToString()
04
If
country =
"Mexico"
Then
05
06
Dim
lb
As
LinkButton =
DirectCast
(e.Row.Cells(0).Controls(0), LinkButton)
07
lb.Visible =
False
08
ElseIf
country =
"Germany"
Then
09
e.Row.Cells(0).Enabled =
False
10
11
End
If
12
End
If
13
End
Sub
14
15
Protected
Function
ShowHide(country
As
Object
)
As
Boolean
16
If
country.ToString() =
"France"
Then
17
Return
False
18
Else
19
Return
True
20
End
If
21
End
Function
0 comments:
Post a Comment