Solved

Disable gridview row if exists in another control

Posted on 2006-06-08
3
959 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Setting up a IIS 8 Web Server to send and receive XML files 7 125
ASP.NET data base connection 35 95
asp.net mvc5 6 34
Delete command SQLdatasource in asp.net, vb 2 29
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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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