472,102 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 software developers and data experts.

disable select button in a gridview...

hi all...
does anyone know how we can disable a button in a grid view when neede???
i have grid that willdisplay some database data...
it also has a button...and i need it enabled or disabled depending upon the
value in another cell in the grid...how can i access that button and make it disabled...
please help...
thanx and regards...
sand...
Jan 19 '07 #1
3 9653
hi all...
does anyone know how we can disable a button in a grid view when neede???
i have grid that willdisplay some database data...
it also has a button...and i need it enabled or disabled depending upon the
value in another cell in the grid...how can i access that button and make it disabled...
please help...
thanx and regards...
sand...
OK, I assume that you use ASP.NET 2003 with C#.
You can manage the grid's controls at grid databound event.
see this sample,
I Fill dataset11 with Categories table from Northwind database.
I Bind dataset11 to datagrid1.

AT HTML........
Expand|Select|Wrap|Line Numbers
  1. <asp:DataGrid id=DataGrid1 style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" Width="464px" DataSource="<%# dataSet11 %>" DataKeyField="CategoryID" DataMember="Categories" AutoGenerateColumns="False">
  2.     <Columns>
  3.         <asp:BoundColumn DataField="CategoryID" SortExpression="CategoryID" HeaderText="CategoryID"></asp:BoundColumn>
  4.         <asp:BoundColumn DataField="CategoryName" SortExpression="CategoryName" HeaderText="CategoryName"></asp:BoundColumn>
  5.         <asp:BoundColumn DataField="Description" SortExpression="Description" HeaderText="Description"></asp:BoundColumn>
  6.         <asp:TemplateColumn>
  7.             <ItemTemplate>
  8.                 <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
  9.             </ItemTemplate>
  10.         </asp:TemplateColumn>
  11.     </Columns>
  12. </asp:DataGrid>
  13.  
AT CS...
Expand|Select|Wrap|Line Numbers
  1. private void Page_Load(object sender, System.EventArgs e)
  2. {
  3.     sqlDataAdapter1.Fill(dataSet11);
  4.     DataGrid1.DataBind();
  5. }
  6.  
Expand|Select|Wrap|Line Numbers
  1. private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  2. {
  3.     if ((e.Item.ItemType == ListItemType.Item) ||
  4.         (e.Item.ItemType == ListItemType.AlternatingItem) ||
  5.         (e.Item.ItemType == ListItemType.SelectedItem))
  6.     {
  7.         decimal ID= decimal.Parse (((DataRowView)e.Item.DataItem)["CategoryID"].ToString());
  8.         if(ID<5)
  9.             ((Button)e.Item.FindControl("Button1")).Enabled = false;
  10.     }
  11. }
  12.  
Hope this help your requirement.

<X>
Jan 20 '07 #2
Thanx a lot 4 ur reply snowwwolf....
but i have another requirement like,
can i have access to a cell in the grid view at d time of databinding???
means at the time of databinding i need to add a button to d grid dynamically based on d value of an existing cell in d grid...
And am having ASP.NET 2005...
Thanx and regards...
sand...
Jan 20 '07 #3
Thanx a lot 4 ur reply snowwwolf....
but i have another requirement like,
can i have access to a cell in the grid view at d time of databinding???
means at the time of databinding i need to add a button to d grid dynamically based on d value of an existing cell in d grid...
And am having ASP.NET 2005...
Thanx and regards...
sand...

Sorry for my late reply, because of my weekend days.
try like this, may be it ok!
Expand|Select|Wrap|Line Numbers
  1. Button b;
  2. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  3. {
  4.      if (e.Row.RowType == DataControlRowType.DataRow)
  5.      {
  6.          decimal id = Convert.ToDecimal(e.Row.Cells[0].Text);
  7.          if (id < 5)
  8.          {
  9.              b = new Button();
  10.              b.Text = "New Button";
  11.              e.Row.Cells[3].Controls.Add(b);
  12.          }
  13.      }
  14. }
  15.  

<X>
Jan 22 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Terry | last post: by
5 posts views Thread by Michael Fällgreen | last post: by
1 post views Thread by Mike P | last post: by
3 posts views Thread by =?Utf-8?B?UGxhdGVyaW90?= | last post: by
reply views Thread by =?Utf-8?B?R3JpZFZpZXcgTmV3Ymll?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.