Link to home
Start Free TrialLog in
Avatar of mooka
mooka

asked on

Disable gridview row if exists in another control


Hi All,

I have a gridview that is populated from SQL. I have a ResourceID bound to the DataKey attribute of the gridview. When the item is selected in the gridview, i add it to another control, then disable the row in the gridview.

However, when the gridview is repopulated by another search, I need to check to see whether the item already exists in the other control (List view) and disable that row if so.

I need to search the control for the ResourceID in all the listitem.value, but cant seem to get the value of the DataKey on row creation or databound.... Seems like I only have access to it on edit, index change.. etc


any ideas?

Thanks,

Mooka
Avatar of Edwin_C
Edwin_C
Flag of Hong Kong image

Based on your approach, here is a feasible solution.

You can put your data in a datatable object.

Then add a column (say IsSelected) to the table.
      DataColumn dc = new DataColumn("IsSelected", typeof(bool));
      dc.DefaultValue = false;
      myDataTable.Columns.Add(dc);

Then you have two choices.  One is to disable selection from the gridview.  This can be done by disabling the linkbutton for selection depending on the value of the boundfield "IsSelected".

The second option is to show the unselected choices only by creating a dataview with filter "IsSelected=false" and bind the dataview to your datagrid.

Whatever options you take, you need to save your datatable in a ViewState variable so that you can refresh the datagrid after user making a choice.  Note that you can use the same datatable to create another dataview with filter "IsSelected=true" for binding to your datalist for selected items.

Another approach for allowing multiple selections is to add an unbound column in your gridview and add a checkbox in the column.  Then when postback, you can loop through every row and check the value of the checkbox and see which items are selected.  This approach can save a few postbacks.

Hope that helps
Edwin
Avatar of mooka
mooka

ASKER


Thanks for the suggestion, but it seems like I could have a simple solution... something like this:

 Protected Sub gvSearch_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim item As New ListItem
        item.Value = Me.gvSearch.SelectedDataKey(0)
        item.Text = Me.gvSearch.SelectedRow.Cells(1).Text & ", " & Me.gvSearch.SelectedRow.Cells(2).Text & " on " & Me.gvSearch.SelectedRow.Cells(3).Text
        If Me.ListBox1.Items.Contains(item) = False Then
            Me.ListBox1.Items.Add(item)
            Me.gvSearch.SelectedRow.Enabled = False
        End If
        Me.ListBox1.Visible = True
    End Sub

If I could run this without having the user have to initiate the task, such as on databound or onrowcreated would be golden. Any way to do that?
ASKER CERTIFIED SOLUTION
Avatar of Edwin_C
Edwin_C
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial