在 AspxGridview 上显示组合

发布于 2024-09-10 22:29:42 字数 3656 浏览 1 评论 0原文

我在 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.**
  1. 有什么问题吗?
  2. 如何解决这个问题呢?
  3. 如何在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.**
  1. What's the problem?
  2. How to solve this problem?
  3. How to bind cell combo on aspx gridview?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥︴琐丶欲为 2024-09-17 22:29:42

这就是这个问题的原因。您正在检查“CategoryID”字段名称。但是 ComboBox 列是为“CategoryName”字段创建的:

if (!ASPxGridView1.IsEditing || e.Column.FieldName != "CategoryID") return;
ASPxComboBox combo = e.Editor as ASPxComboBox;

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;
带上头具痛哭 2024-09-17 22:29:42

我们发布了一个示例项目,展示如何在插入模式下实现依赖组合:

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.

唔猫 2024-09-17 22:29:42

在 OnRowDataBound 事件(或 AspxGridview 等效事件)中绑定

Bind in the OnRowDataBound event (or AspxGridview equivalent)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文