Programatically changing ASP Gridview column header text breaks sorting - asp.net

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)";
}

Related

Hide Columns in gridview

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

GridView column losing hyperlink when looping through rows

I have an asp.net page written with vb.net that has a gridview table. The table is setup to have a checkbox column for each row. If this checkbox is checked for a row, I do some additional things. One of the other columns also includes a hyperlink on the text for each cell in that column.
I have a button on the page that will loop through and programmatically check each one of the checkboxes. The button does this correctly; however, it also removes the hyperlink from the other column for some reason. Any ideas why?
Here is the code where I set the hyperlinks for the hyperlink column:
Protected Sub gridData_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridData.RowDataBound
'When a row is added to the data grid view we need to add the hyperlink to the zipname column
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cell As TableCell = e.Row.Cells(4)
Dim lnk As HyperLink = New HyperLink
'Set properties of the link
lnk.NavigateUrl = cell.Text
lnk.Text = cell.Text
'Format the filename, filepath, zipname, and username to be lowercase
e.Row.Cells(1).Text = LCase(e.Row.Cells(1).Text)
e.Row.Cells(3).Text = LCase(e.Row.Cells(3).Text)
e.Row.Cells(4).Text = LCase(e.Row.Cells(4).Text)
e.Row.Cells(5).Text = LCase(e.Row.Cells(5).Text)
'Add the hyperlink
cell.Controls.Clear()
cell.Controls.Add(lnk)
End If
End Sub
Here is the code for the check all button:
Protected Sub cmdCheckAll_Click(sender As Object, e As EventArgs) Handles cmdCheckAll.Click
'Check all of the download checkboxes
For Each row As GridViewRow In gridData.Rows
CType(row.FindControl("CheckBox1"), CheckBox).Checked = True
Next
End Sub
As is the usual case with Dynamic Controls, All the dynamically created Hyperlinks in RowDataBound event needs to be recreated every postback.
So, there are 2 options.
1.) Either bind your GridView on every postback :
protected void Page_Load(object sender, EventArgs e)
{
gridData.DataBind();
}
OR
2.) Turn off ViewState for your GridView::
<asp:GridView ID="gridData" EnableViewState="false" ... />.
On using any of above options, the OnRowDataBound event is executed each time postback happens, thus recreating the Dynamic controls. Now you can access the Hyperlinks in your button click event.
Check properly which one of above options is best for your application.

Using panels in ASP.net

i created a textbox, a button and a panel in a page. So my aim is to create links which will be put inside a panel. However, it turns out that every time i add another one, it seems to replace the previously created link. Is there a way to just add the links, not replacing previously created links? I really don't have a background that much in ASP.
This is the code I researched.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim link As New HyperLink()
Dim ltr As New Literal()
link.Text = TextBox1.Text
link.NavigateUrl = "Default.aspx?field1=" + TextBox2.Text + " "
ltr.Text = "<br/>"
Panel1.Controls.Add(ltr)
Panel1.Controls.Add(link)
Please help me. Thanks!
As said by #Rex, when post back occurs dynamically controls will be removed from the page until we retain them in Init Event.
While adding controls to PlaceHolder, save the same data like ID, Text..etc. in a object like session variable and retrieve the same data in Page Init event and add to the place holder.
protected void Page_Init(object sender, EventArgs e)
{
// Retrieve from session variable and add the same data to the place holder.
}

Gridview - Greying out checkboxes once one is checked

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...

How to get Command Functionality from a Panel

Is there a Panel or any container with CommandName, CommandArguments instead of using Buttons (LinkButton, ImageButton, ...)?
I want to add a column in a GridView to make selection for the row, the whole cell's rectangle instead of a Select link.
You can make (almost) anything implement CommandName and CommandArguments by implementing the IButtonControl interface. You will probably also want to implement the IPostBackEventHandler interface.
This article covers in detail exactly what you are asking about, generally: making a Panel into a command control. It's not entirely trivial.
But making a table row clickable is a lot easier, though, esp. using jQuery. See this article. You just bind an event to the row, and go from there. In this example, they're redirecting to the url for a link in the row on a click event. You could just as easily do something else like call __doPostBack to cause an async postback and run arbitrary server code, e.g.
Here is another way how you can make a GridView-Cell as Sort-Column(see in RowCreated, the rest is sample-data):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindGridView()
End If
End Sub
Private Sub BindGridView()
Dim source As New List(Of String)(New String() {"1. row", "2. row", "3. row", "4. row", "5. row"})
Me.GridView1.DataSource = source
Me.GridView1.DataBind()
End Sub
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim cell As New TableCell
If e.Row.RowType = DataControlRowType.DataRow Then
cell.Text = "select"
cell.ToolTip = "click to select row"
cell.Attributes("onmouseover") = "this.style.cursor='pointer';"
cell.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(DirectCast(sender, GridView), "Select$" & e.Row.RowIndex)
End If
e.Row.Cells.Add(cell)
End Sub
Edit: if you get a "Invalid postback or callback argument"-Exception from ASP.Net, you can either set EnableEventValidation="False" or add following to your page's codebehind:
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
ClientScript.RegisterForEventValidation(GridView1.UniqueID, "Select$" & row.RowIndex)
End If
Next
MyBase.Render(writer)
End Sub
If you need C#, copy all into: http://www.developerfusion.com/tools/convert/vb-to-csharp/

Resources