Solved

Disable gridview row if exists in another control

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

13 Experts available now in Live!

Get 1:1 Help Now