在 AspxGridview 上显示组合
我在 northwind
数据库上工作。在我的 AspxGridview
中,我想显示 comboBox
。我在后端填充网格 C#
我也希望我的组合将填充后端。
<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>
要填充网格,我使用下面的 C# 语法。
DataClasses1DataContext db = new DataClasses1DataContext();
var r = from p in db.Categories
select p;
ASPxGridView1.DataSource = r;
ASPxGridView1.DataBind();
要填充组合的 gridview 单元格,我使用下面的 C# 语法
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();
}
当我运行代码时 AspxGridview
填充良好但是当我单击网格左侧的“编辑”或“新命令”时,会显示以下错误消息:
**Object reference not set to an instance of an object.**
- 有什么问题吗?
- 如何解决这个问题呢?
- 如何在aspx gridview上绑定单元格组合?
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.**
- What's the problem?
- How to solve this problem?
- How to bind cell combo on aspx gridview?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是这个问题的原因。您正在检查“CategoryID”字段名称。但是 ComboBox 列是为“CategoryName”字段创建的:
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:
我们发布了一个示例项目,展示如何在插入模式下实现依赖组合:
http://www.devexpress.com/Support/Center/ViewIssue.aspx?issueid=Q102130
我希望这个项目对您有所帮助。
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.
在 OnRowDataBound 事件(或 AspxGridview 等效事件)中绑定
Bind in the OnRowDataBound event (or AspxGridview equivalent)