I have a GridView which I fill with a DataTable on RunTime, but I want to hide one of the columns when it's already loaded in the GridView, I've tried:
Me.GridView1.Columns(0).Visible = False
but it gives me an exception of Out of range.
Do you know any other way to do it?
Try this:
Private Sub GridView1_DataBound(sender As Object, e As EventArgs) Handles GridView1.DataBound
GridView1.Columns(0).Visible = False
End Sub
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[index].Visible = false;
}
Thanks everybody for helping, I tried grid.ros.cells(i).visible = false but it did not worked but reading I found that I have to hide the header row too, so I made this function to hide any row in any grid
Private Sub HideColumn(ByRef grid As GridView, ByVal x As Integer)
If grid.Rows.Count > 0 Then
grid.HeaderRow().Cells(x).Visible = False
For i As Integer = 0 To grid.Rows.Count - 1
grid.Rows(i).Cells(x).Visible = False
Next
End If
End Sub
Related
I'm new to all of this code, so please be patient.
On my gridview, I am trying to make it so, once I check on of the checkboxes, the others get greyed out, making it impossible to select another. How would I code this?
Thanks in advance.
PS: I'm using vb, rather than C#.
Edit: Here is the code I want to run.
Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs) Handles btnOK.Click
Dim TxtbxID As TextBox
Dim RB As System.Web.UI.WebControls.RadioButton
For Each row As GridViewRow In GridView1.Rows
RB = row.FindControl("select")
If RB.Checked Then
Me.txtHiddenProblem.Text = row.Cells(3).Text & " on " & row.Cells(5).Text
TxtbxID = FormView1.FindControl("ProblemTextbox")
TxtbxID.Text = txtHiddenProblem.Text
End Sub
Take a look at this article: http://www.ezzylearning.com/tutorial.aspx?tid=5187857
It has some lines of code which help you determine the particular gridview row the checkbox element was associated with...
public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
{
CheckBox chkStatus = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkStatus.NamingContainer;
//other code
}
Now all we need to do is when the checkbox element is checked, iterate through the grid, find the checkbox, and disable them if its not the same as the one which caused the event...
in GridView:
proctected sub gridview.DataBound(Byval sender as object, byval e assystemEventArgs) handles gridview.databound
{
}
how to use DataBound in DATAGRID??
You can try with this code based on OnItemDataBound
<asp:DataGrid
id="ItemsGrid"
runat="server"
OnItemDataBound="Item_Bound"
.../>
//Code behind
void Item_Bound(Object sender, DataGridItemEventArgs e)
{
Label1.Text = Label1.Text + " " + e.Item.ItemIndex;
}
To be honest, your question doesn't make much sense currently. But if you want to handle the GridView's DataBound event (as opposed to the RowDataBound event):
Protected Sub Gridview1_DataBound(sender As Object, e As System.EventArgs) Handles Gridview1.DataBound
Dim grid = DirectCast(sender, GridView)
Dim dataSource As Object = grid.DataSource
For Each row As GridViewRow In grid.Rows
' do something, looping all rows in the grid with RowType=DataRow '
Next row
End Sub
I'm making a large data entry web form using a DataList.
I have three sets of questions that I store some info about in ASP HiddenFields. I want to color the back of these data list items based on the values of those HiddenFields, but in the ItemCreated event my DataListItemEventArgs are all Nothing or Empty.
I had this working using ItemDataBound, but my rows would lose color upon postback.
This is what I was using in ItemDataBound:
Protected Sub CategoryColors(ByVal sender As Object, ByVal e As DataListItemEventArgs) Handles TheTable.ItemDataBound
Dim v = CType(e.Item.FindControl("CtType"), HiddenField).Value
Try
If v = 1 Then
e.Item.BackColor = Drawing.Color.LightBlue
ElseIf v = 2 Then
e.Item.BackColor = Drawing.Color.Khaki
ElseIf v = 3 Then
e.Item.BackColor = Drawing.Color.LightGreen
Else
e.Item.BackColor = Drawing.Color.Red
End If
Catch ex As Exception
End Try
End Sub
Copying the body of that sub to the ItemCreated handler doesn't do anything. Can I do this same thing a different way? Thanks much for your help.
I have gridview bound to an Entity Data source which works no problem, however when I try to programatically change a header columns text, it appears to break the styling and will not allow sorting either, below is how I am trapping and changing the Header row column text.
Does anyone have any ideas?
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv1.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
'retrieve the values from the userdeftable
e.Row.Cells(6).Text = App.Session.Company.UserDef3
End If
End Sub
Use the HeaderText-property of the column.
Me.gv1.Columns(6).HeaderText = App.Session.Company.UserDef3
Use the Sorted event...
Example of how to toggle HeaderText to show the order of the data.
protected void gvCurrCheckIns_Sorted(object sender, EventArgs e)
{
if (gvCurrCheckIns.Columns[8].HeaderText.Contains("(DESC)"))
gvCurrCheckIns.Columns[8].HeaderText = "Checked IN (ASC)";
else
gvCurrCheckIns.Columns[8].HeaderText = "Checked IN (DESC)";
}
Ok so I've got my Drop down list databound to an sql data source, now I need to change the data in a few of the fields before it gets displayed. I've been messing with this all morning and I can't find anything useful, this is what I've got so far and it's clearly not working...
Protected Sub ddlBookType_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBookType.DataBound
'ddlBookType.Items.Insert(0, "Any")
Dim i As Integer
For i = 0 To ddlBookType.Items.Count - 1
If ddlBookType.Items(i).Attributes.Equals("mod_cook") Then
ddlBookType.Items(i).Text.Replace("mod_cook", "Modern Cooking")
End If
Next
End Sub
Hey I've just done it in C# and converted it into VB (I hope it's right).
Here's the code (
Protected Sub ddlBookType_DataBound(sender As Object, e As EventArgs)
Dim ddlBookType As DropDownList = DirectCast(sender, DropDownList)
For Each item As ListItem In ddlBookType.Items
If item.Text = "mod_cook" Then
item.Text = "Modern Cooking"
End If
Next
End Sub
And here's the original C# code
protected void ddlBookType_DataBound(object sender, EventArgs e)
{
DropDownList ddlBookType = (DropDownList)sender;
foreach (ListItem item in ddlBookType.Items)
{
if (item.Text == "mod_cook")
{
item.Text = "Modern Cooking";
}
}
}