我文本框,单选按钮和组合框的数据添加到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
系列文章
更多
同类精品
更多
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20