Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Disable gridview row if exists in another control

Posted on 2006-06-08
3
Medium Priority
?
964 Views
Last Modified: 2011-10-03

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
0
Comment
Question by:mooka
  • 2
3 Comments
 
LVL 16

Expert Comment

by:Edwin_C
ID: 16867164
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
0
 

Author Comment

by:mooka
ID: 16867338

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?
0
 
LVL 16

Accepted Solution

by:
Edwin_C earned 2000 total points
ID: 16870392
Hi Mooka

I think my solution is simpler than yours.  What you suggested needs a postback for every selection made.  That means a flash of the screen.  Also if your gridview is large, user has to scroll down to where they made the last selection and then continue.  Also you have to take care about how to let users correct their selection i.e. to unselect a selection.  All this makes your solution complex although it is possible.

I think the best way is to add a "select" column to the grid view like this.

        <asp:GridView ID="GridView1" runat="server" DataKeyNames="id">
            <Columns>
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkbox" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Then you can add a submit button and do this when it is clicked.

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow r in GridView1.Rows)
        {
            if (((CheckBox)r.FindControl("chkbox")).Checked)
            {
                int key = (int)GridView1.DataKeys[r.RowIndex].Value;  //this is the key value of the selected row
            }
        }
    }

Edwin
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question