pensiongenius
asked on
Capture value from combo box column in datagridview
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:
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.
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(mydatagrid view.Item( 13, intX).Value.ToString))
'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)
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
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(mydatagrid
hi..Please try with this example
SqlConnection sqlcon = new SqlConnection(Configuratio nManager.C onnectionS trings["Db Connect"]. 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.DataSourc e = ds;
Grid_viewprofile.DataBind( );
}
}
protected void Grid_viewprofile_RowComman d(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Close")
{
// Panel1.Visible = false;
}
if (e.CommandName == "Move")
{
GridViewRow gvRow = (GridViewRow)(((Button)e.C ommandSour ce).Naming Container) ;
DropDownList lstuserName = (DropDownList)gvRow.FindCo ntrol("ddl _groub");
Response.Write("Selected Value : " + lstuserName.SelectedValue + " " + "Selected Item : " + lstuserName.SelectedItem.T ext);
}
}
public DataSet ddlist(string Qry)
{
DataSet ds = new DataSet();
SqlDataAdapter sqladp = new SqlDataAdapter(Qry, sqlcon);
sqladp.Fill(ds);
return ds;
}
protected void Grid_viewprofile_RowDataBo und(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
DropDownList ddl = (DropDownList)e.Row.FindCo ntrol("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_viewpro file_RowCo mmand"
onrowdatabound="Grid_viewp rofile_Row DataBound" >
<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>
SqlConnection sqlcon = new SqlConnection(Configuratio
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.DataSourc
Grid_viewprofile.DataBind(
}
}
protected void Grid_viewprofile_RowComman
{
if (e.CommandName == "Close")
{
// Panel1.Visible = false;
}
if (e.CommandName == "Move")
{
GridViewRow gvRow = (GridViewRow)(((Button)e.C
DropDownList lstuserName = (DropDownList)gvRow.FindCo
Response.Write("Selected Value : " + lstuserName.SelectedValue + " " + "Selected Item : " + lstuserName.SelectedItem.T
}
}
public DataSet ddlist(string Qry)
{
DataSet ds = new DataSet();
SqlDataAdapter sqladp = new SqlDataAdapter(Qry, sqlcon);
sqladp.Fill(ds);
return ds;
}
protected void Grid_viewprofile_RowDataBo
{
if (e.Row.RowType == DataControlRowType.DataRow
{
DropDownList ddl = (DropDownList)e.Row.FindCo
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
onrowdatabound="Grid_viewp
<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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Experts. This solution was concise and to the point and did exactly what I needed.
-Kishan