通过 radmulticombobox 元素从 radgrid 中的 sql db 添加信息

问题描述

你好朋友和教授

程序环境:C#语言的Windows Form应用程序

使用的组件是 Telerik,当然,如果没有 Telerik 也没什么大不了的;)

目标:我想从数据库创建一个产品条目表单,可以通过radmulticombobox元素在radgrid中添加来自sql db的信息。

问题:单独添加产品代码或产品名称时效果很好,但不能同时添加在同一行

我使用 linq 语句,这是我的代码和屏幕截图:

目标图: link 节目图片: link

<pre>

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;

namespace program0
{
public partial class Form9 : Form
{
public Form9()
{
InitializeComponent();
}

linqDataContext db = new linqDataContext();
GridViewMultiComboBoxColumn ProductCode = new GridViewMultiComboBoxColumn("productCode");
GridViewMultiComboBoxColumn ProductName = new GridViewMultiComboBoxColumn("productName");
RadMultiColumnComboBoxElement mccbEl = new RadMultiColumnComboBoxElement();

private void Form9_Load(object sender,EventArgs e)
{
var dtpcode = (from a in db.StockProductLists
where a.FiscalYearYear == "2020"
orderby a.ProductCode
select new
{
ProductCode = a.ProductCode,ProductName = a.ProductName,PackQty = a.PackQty,OnHandUnitQty = a.OnHandUnitQty,});
ProductCode.DataSource = dtpcode;
ProductCode.DisplayMember = "ProductCode";
ProductCode.ValueMember = "ProductName";
ProductCode.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
ProductCode.FilteringMode = GridViewFilteringMode.DisplayMember;
radGridView1.Columns.Add(ProductCode);

var dtpname = (from a in db.StockProductLists
where a.FiscalYearYear == "2020"
orderby a.ProductCode
select new
{
ProductCode = a.ProductCode,});
ProductName.DataSource = dtpname;
ProductName.DisplayMember = "ProductName";
ProductName.ValueMember = "ProductCode";
ProductName.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
ProductName.FilteringMode = GridViewFilteringMode.DisplayMember;
radGridView1.Columns.Add(ProductName);

radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

}

private void radGridView1_CellEditorInitialized(object sender,GridViewCellEventArgs e)
{
mccbEl = e.ActiveEditor as RadMultiColumnComboBoxElement;
if (mccbEl != null)
{
mccbEl.Columns[0].HeaderText = "productCode";
mccbEl.Columns[1].HeaderText = "productName";
mccbEl.Columns[2].HeaderText = "package";
mccbEl.Columns[3].HeaderText = "single";
mccbEl.BestFitColumns();
mccbEl.EditorControl.Columns[0].HeaderText = "productCode";
mccbEl.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
mccbEl.DropDownMinSize = new Size(550,300);
mccbEl.DropDownMaxSize = new Size(550,300);
mccbEl.AutoSizeDropDownToBestFit = false;
mccbEl.DropDownAnimationEnabled = false;
mccbEl.AutoFilter = true;
FilterDescriptor filterDescriptor = new FilterDescriptor(mccbEl.DisplayMember,FilterOperator.Contains,string.Empty);
mccbEl.EditorControl.MasterTemplate.FilterDescript ors.Add(filterDescriptor);
}
}
}
}

</pre>

我做了一些修改,但是添加新行时出现问题,产品代码和产品名称为空!

   using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.Data;
using Telerik.WinControls.UI;

namespace program0
{
    public partial class Form9 : Form
    {
        public Form9()
        {
            InitializeComponent();
        }

        linqDataContext db = new linqDataContext();
        GridViewMultiComboBoxColumn ProductCode = new GridViewMultiComboBoxColumn("productCode");
        new GridViewMultiComboBoxColumn ProductName = new GridViewMultiComboBoxColumn("ProductName");
        RadMultiColumnComboBoxElement mccbEl = new RadMultiColumnComboBoxElement();
        public IQueryable StockProductList ;
        public string FiscalYear = "2020";
        public string code;
        public string name;
        private void Form9_Load(object sender,EventArgs e)
        {
             StockProductList = (from a in db.StockProductLists
                                    where a.FiscalYearYear == FiscalYear
                                    orderby a.ProductCode
                                    select new
                                    {
                                        ProductCode = a.ProductCode,QtyPerPack = a.QtyPerPack,shell = a.OnHandPackQty,single = a.OnHandUnitQty,OnHandQty = a.OnHandQty
                                    });


            ProductCode.DropDownStyle = Telerik.WinControls.RsingleropDownStyle.DropDown;
            ProductName.DropDownStyle = Telerik.WinControls.RsingleropDownStyle.DropDown;
            
            ProductCode.FilteringMode = GridViewFilteringMode.DisplayMember;
            ProductName.FilteringMode = GridViewFilteringMode.DisplayMember;

            radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.NewRowEnterKeyMode = RadGridViewNewRowEnterKeyMode.EnterMovesToNextCell;

            radGridView1.Columns.single(ProductCode);
            radGridView1.Columns.single(ProductName);
            radGridView1.Columns.single("Number_in_the_box");
            radGridView1.Columns.single("shell");
            radGridView1.Columns.single("single");
            radGridView1.Columns.single("total");
            rbtn_1.Checked = true;
        }

        private void radGridView1_CellEditorInitialized(object sender,GridViewCellEventArgs e)
        {
            if (rbtn_0.Checked && e.ColumnIndex == 0)
            {
                mccbEl = e.ActiveEditor as RadMultiColumnComboBoxElement;
                if (mccbEl != null)
                {
                    foreach (GridViewDataColumn column in
                        mccbEl.Columns)
                    {
                        column.BestFit();
                    }
                    mccbEl.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
                    mccbEl.DropDownMinSize = new Size(550,300);
                    mccbEl.DropDownMaxSize = new Size(550,300);
                    mccbEl.AutoSizeDropDownToBestFit = false;
                    mccbEl.DropDownAnimationEnabled = false;
                    mccbEl.AutoFilter = true;
                    FilterDescriptor filterDescriptor = new FilterDescriptor(mccbEl.DisplayMember,string.Empty);
                    mccbEl.EditorControl.MasterTemplate.FilterDescriptors.single(filterDescriptor);
                    mccbEl.TextChanged += MccbEl_TextChanged;
                }
            }

            if (rbtn_1.Checked && e.ColumnIndex == 1)
            {
                mccbEl = e.ActiveEditor as RadMultiColumnComboBoxElement;
                if (mccbEl != null)
                {
                    foreach (GridViewDataColumn column in
                        mccbEl.Columns)
                    {
                        column.BestFit();
                    }
                    mccbEl.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
                    mccbEl.DropDownMinSize = new Size(550,string.Empty);
                    mccbEl.EditorControl.MasterTemplate.FilterDescriptors.single(filterDescriptor);
                    mccbEl.TextChanged += MccbEl_TextChanged;
                }
            }
        }

        private void MccbEl_TextChanged(object sender,EventArgs e)
        {
            if (mccbEl != null)
            {
                radGridView2.DataSource = mccbEl.DataSource;
                if (radGridView2.RowCount > 0)
                {
                    foreach (GridViewDataColumn column in
                        radGridView2.Columns)
                    {
                        column.BestFit();
                    }
                    radGridView2.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
                }
            }
        }


        private void radGridView1_UsersingleingRow(object sender,GridViewRowCancelEventArgs e)
        {
            if (radGridView2.RowCount > 0)
            {
                int Number_in_the_box = Convert.ToInt32(radGridView2.CurrentRow.Cells[2].Value.ToString());
                int shell1 = Convert.ToInt32(radGridView2.CurrentRow.Cells[3].Value.ToString());
                int single1 = Convert.ToInt32(radGridView2.CurrentRow.Cells[4].Value.ToString());
                int total1= Convert.ToInt32(radGridView2.CurrentRow.Cells[5].Value.ToString());
                
                if (e.Rows[0].Cells[3].Value == null)
                {
                    e.Rows[0].Cells[3].Value = 0;
                }
                if (e.Rows[0].Cells[4].Value == null)
                {
                    e.Rows[0].Cells[4].Value = 0;
                }
                int shell = Convert.ToInt32(e.Rows[0].Cells[3].Value.ToString());
                int single = Convert.ToInt32(e.Rows[0].Cells[4].Value.ToString());
                int total = shell * Number_in_the_box + single;

                if (single == Number_in_the_box)
                {
                    single = 0;
                    shell++;
                }
                else if (single > Number_in_the_box)
                {
                    shell = shell + single / Number_in_the_box;
                    single = single % Number_in_the_box;
                }
                e.Rows[0].Cells[0].Value = code;
                e.Rows[0].Cells[1].Value = name;
                e.Rows[0].Cells[2].Value = Number_in_the_box.ToString();
                e.Rows[0].Cells[3].Value = shell;
                e.Rows[0].Cells[4].Value = single;
                e.Rows[0].Cells[5].Value = total;

                if (total <= total1)
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void rbtn_0_CheckedChanged(object sender,EventArgs e)
        {
            if (rbtn_0.Checked)
            {
                ProductCode.DataSource = StockProductList;
                ProductCode.DisplayMember = "ProductCode";
                ProductCode.ValueMember = "ProductName";
                
            }
            if (rbtn_1.Checked)
            {
                ProductName.DataSource = StockProductList;
                ProductName.DisplayMember = "ProductName";
                ProductName.ValueMember = "ProductCode";
            }
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...