• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

我文本框,单选按钮和组合框的数据添加到C#disktop app的gridview列

用户头像
it1352
帮助1

问题说明

我在gridview中创建列来显示值。







这是我的代码: -



i crate columns in gridview to display values.



this is my code :-

r = dt.NewRow();
                r[0] = dt.Rows.Count   1;
                r[1] = txt_Job_card.Text;
                r[2] = txt_Value_Part.Text;

                if (radioButton_Part.Checked == true)
                {
                    r[3] = CB_Auto_parts.Text;
                    r[5] = x;
                }
                if (radioButton_repair.Checked == true)
                {
                    r[4] = CB_Repair_type.Text;
                }
                r[6] = CB_Providers_desc.Text;
                r[7] = CB_Hand_installation.Text;
                r[8] = txt_Notes.Text;
                r[9] = CB_Auto_parts.SelectedValue;
                r[10] = CB_Repair_type.SelectedValue;
                r[11] = CB_Providers_desc.SelectedValue;
                r[12] = CB_Hand_installation.SelectedValue;

                dt.Rows.Add(r);
                dgv_Add_job_card.DataSource = dt;
             
                ClearText();

                txt_Total_Values.Text = (from DataGridViewRow row in dgv_Add_job_card.Rows
                                         where row.Cells[2].FormattedValue.ToString() != string.Empty
                                         select Convert.ToDouble(row.Cells[2].FormattedValue)).Sum().ToString();
            }





我的尝试:



i尝试使用此代码,但它不起作用,我有错误,找不到第0列。



What I have tried:

i tried to use this code but its not work and i have error , Cannot find column 0.

正确答案

#1
我建议使用 BindingList 并将你的值放在一个类中,这是一个极简主义的例子:

I would recommend using BindingList and putting your values in a class, here is a minimalist example:
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestForm1
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// The binding list reference.
        /// </summary>
        private BindingList<MyClass> masterBindingList;

        public Form10()
        {
            InitializeComponent();
            this.Init();
        }

        private void Init()
        {
            this.masterBindingList = new BindingList<MyClass>();
            this.dataGridViewMaster.DataSource = this.masterBindingList;
        }

        private void ButtonAddRowClick(object sender, EventArgs e)
        {
            var rowIndex = this.dataGridViewMaster.RowCount;
            this.AddRow(rowIndex);
            // this.UpdateRow(rowIndex);
            this.ScrollToRow(rowIndex);
        }

        /// <summary>
        /// Add new row via the BindingList.
        /// </summary>
        private void AddRow(int rowIndex)
        {
            this.masterBindingList.Add(new MyClass { Title = "Row "   rowIndex.ToString() });
        }

        private void ScrollToRow(int rowIndex)
        {
            this.dataGridViewMaster.ClearSelection();
            this.dataGridViewMaster.FirstDisplayedScrollingRowIndex = rowIndex;
            this.dataGridViewMaster.Focus();
        }
        /// <summary>
        /// Simple class with one string.
        /// </summary>
        public class MyClass
        {
            public string Title { get; set; }
        }
    }
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfjahi
系列文章
更多 icon
同类精品
更多 icon
继续加载