Solved

Disable gridview row if exists in another control

Posted on 2006-06-08
3
955 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

929 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now