Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Capture value from combo box column in datagridview

Posted on 2014-02-18
4
Medium Priority
?
1,612 Views
Last Modified: 2014-02-19
I have a datagridview that is databound.  Once the view is populated via call to a SQL DB I append a combobox column to the end of it. using this code:

'Code to add column to the end of the databound datagridview
CreateUnboundDropDownColumn(mydatagridview, "Assigned", mydatagridview.ColumnCount)

'Sub that adds the column on
Private Sub CreateUnboundDropDownColumn(ByVal DataGridView As DataGridView, ByVal ColumnName As String, ByVal ColumnIndex As Integer)

 Dim ComboboxColumn As New DataGridViewComboBoxColumn


        With ComboboxColumn
            .HeaderText = ColumnName
            .Name = "col_" & ColumnName
            .Items.Add("Value1")
            .Items.Add("Value2")
            .Items.Add("Value3")
            .Items.Add("Value4")
            .Items.Add("Value5")

        End With

        ' Add the combobox column to the control.
        DataGridView.Columns.Insert(ColumnIndex, ComboboxColumn)

Open in new window


This all works fine but when I then try to capture the value in this new column to pass to a class that saves back to the database I get the famous "object not set to an instance of an object".  

I set the value in this manner.

For intX As Integer = 0 To mydatagridview.Rows.Count - 1

                            Dim Assigned As String

If dgv_CashAdjustment.Item(13, intX).Value.ToString = "" Or dgv_CashAdjustment.Item(13, intX).Value Is Nothing Then
                                Assigned = ""
                            Else
                                Assigned = dgv_CashAdjustment.Item(13, intX).Value.ToString()
                            End If

mysaveclass.Assigned=Assigned

mysaveclass.PerformSaveRoutine

Open in new window


I have double checked that column 13 is the correct column index so that isn't it.

The error occurs when I assign the value because I get the same error even if I simply try to read the value (ie. MessageBox.Show(mydatagridview.Item(13, intX).Value.ToString))
0
Comment
Question by:pensiongenius
[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
4 Comments
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 39869430
since there is null value in mydatagridview.Item(13, intX).Value, because of this you are getting object reference error. First you have to check for null and then convert to String.

-Kishan
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39869712
hi..Please try with this example

SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnect"].ToString());

    SqlDataAdapter sqladp = new SqlDataAdapter();
    DataTable dt = new DataTable();
    DataTable dt1 = new DataTable();
    DataTable dt2 = new DataTable();

  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds=new DataSet ();
            ds = ddlist("Select * from UserDetails");
            Grid_viewprofile.DataSource = ds;
            Grid_viewprofile.DataBind();
        }
    }
    protected void Grid_viewprofile_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Close")
        {
        //    Panel1.Visible = false;
        }
        if (e.CommandName == "Move")
        {
           GridViewRow gvRow = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            DropDownList lstuserName = (DropDownList)gvRow.FindControl("ddl_groub");
            Response.Write("Selected Value : " + lstuserName.SelectedValue + " " + "Selected Item : " + lstuserName.SelectedItem.Text);  
        }
    }
    public  DataSet   ddlist(string Qry)
    {
        DataSet ds = new DataSet();
        SqlDataAdapter sqladp = new SqlDataAdapter(Qry, sqlcon);
        sqladp.Fill(ds);
        return ds;
    }
    protected void Grid_viewprofile_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList  ddl = (DropDownList)e.Row.FindControl("ddl_groub");
            ddl.DataSource = ddlist("Select * from UserDetails");
            ddl.DataTextField = "UserName";
            ddl.DataValueField = "ID";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("--Select--", "0"));
        }
    }
}

Client Side  :
<asp:GridView ID="Grid_viewprofile" CellPadding="4" ForeColor="Black"  
        runat="server" AllowPaging="true" PageSize="1"
    AutoGenerateColumns="false" onrowcommand="Grid_viewprofile_RowCommand"
     onrowdatabound="Grid_viewprofile_RowDataBound">
    <Columns>
    <asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="UserName" HeaderText="UserName" />
     <asp:TemplateField HeaderText="Profile_Status">
    <ItemTemplate>
        <asp:DropDownList ID="ddl_groub" runat="server">
        </asp:DropDownList>
        <asp:Button ID="btn_move" runat="server" Text="Move to group"  CommandName="Move" />
        <asp:Button ID="btn_close" runat="server" Text="Close"  CommandName="Close"/>
    </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 39870215
Change line 5 from

If dgv_CashAdjustment.Item(13, intX).Value.ToString = "" Or dgv_CashAdjustment.Item(13, intX).Value Is Nothing Then

to

If String.IsNullOrEmpty(dgv_CashAdjustment.Item(13, intX).Value) Then
0
 

Author Closing Comment

by:pensiongenius
ID: 39870568
Thanks Experts.  This solution was concise and to the point and did exactly what I needed.
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 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