Reputation: 6778
I work on northwind
database. In my AspxGridview
I want to show comboBox
. I fill grid on back end C#
I also want my combo will fill back end.
<dxwgv:ASPxGridView ID="ASPxGridView1" runat="server"
AutoGenerateColumns="False" KeyFieldName="CategoryID"
oncelleditorinitialize="ASPxGridView1_CellEditorInitialize">
<Columns>
<dxwgv:GridViewCommandColumn VisibleIndex="0" Width="80px">
<EditButton Visible="True">
</EditButton>
<NewButton Visible="True">
</NewButton>
<DeleteButton Visible="True">
</DeleteButton>
</dxwgv:GridViewCommandColumn>
<dxwgv:GridViewDataTextColumn Caption="CategoryID" FieldName="CategoryID"
VisibleIndex="1">
</dxwgv:GridViewDataTextColumn>
<dxwgv:GridViewDataComboBoxColumn Caption="CategoryName"
FieldName="CategoryName" VisibleIndex="2">
<PropertiesComboBox TextField="Value" ValueField="key" ValueType="System.Int32">
<ClientSideEvents SelectedIndexChanged="function(s, e) { OnBankChanged(s); }" />
</PropertiesComboBox>
</dxwgv:GridViewDataComboBoxColumn>
<dxwgv:GridViewDataTextColumn Caption="Description" FieldName="Description"
VisibleIndex="3">
</dxwgv:GridViewDataTextColumn>
</Columns>
</dxwgv:ASPxGridView>
To fill grid i use the bellow C# syntax.
DataClasses1DataContext db = new DataClasses1DataContext();
var r = from p in db.Categories
select p;
ASPxGridView1.DataSource = r;
ASPxGridView1.DataBind();
To fill gridview cell of combo i use Bellow C# syntax
protected void ASPxGridView1_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
{
if (!ASPxGridView1.IsEditing || e.Column.FieldName != "CategoryID") return;
ASPxComboBox combo = e.Editor as ASPxComboBox;
if (!(e.KeyValue == DBNull.Value || e.KeyValue == null)) //return;
{
object val = ASPxGridView1.GetRowValuesByKeyValue(e.KeyValue, "CategoryID");
if (val == DBNull.Value) return;
Int32 CategoryID = (Int32)val;
FillCityCombo(combo, CategoryID);
}
combo.Callback += new CallbackEventHandlerBase(cmbBranch_OnCallback);
}
private void cmbBranch_OnCallback(object source, CallbackEventArgsBase e)
{
FillCityCombo(source as ASPxComboBox, Convert.ToInt16(e.Parameter));
}
protected void FillCityCombo(ASPxComboBox cmb, Int32 CategoryID)
{
//cmb.Items.Clear();
//cmb.DataSourceID = "";
DataClasses1DataContext db = new DataClasses1DataContext();
var r = from p in db.Categories
select new { p.CategoryID,p.CategoryName};
cmb.DataSource = r;
cmb.DataBind();
}
When I run the code AspxGridview
fill well but when I click on Edit or New Command on left side of my grid shows me error message below:
**Object reference not set to an instance of an object.**
Upvotes: 0
Views: 7600
Reputation: 11376
We've posted a sample project showing how to implement dependent combos in insert mode at:
http://www.devexpress.com/Support/Center/ViewIssue.aspx?issueid=Q102130
I hope, this project will be helpful to you.
Upvotes: 1
Reputation: 11376
This is the cause of this issue. You are checking for the "CategoryID" field name. But the ComboBox column is created for the "CategoryName" field:
if (!ASPxGridView1.IsEditing || e.Column.FieldName != "CategoryID") return;
ASPxComboBox combo = e.Editor as ASPxComboBox;
Upvotes: 2
Reputation: 7539
Bind in the OnRowDataBound event (or AspxGridview equivalent)
Upvotes: 0