标签: datagridview

将dataGridView中的选定行检索为对象

我有一个这样的课:

public partial class AdressBokPerson
    {
        public long Session { get; set; }
        public string Förnamn { get; set; }
        public string Efternamn { get; set; }
        public string Mail { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

添加到列表中:

private readonly List<AdressBokPerson> _avp = new List<AdressBokPerson>();
Run Code Online (Sandbox Code Playgroud)

通过绑定到dataGridView,如下所示:

dataGridView1.DataSource = _avp;
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.

这是我的问题:

当选择dataGridView中的行时,如何找出选择的"哪个"对象.我需要以某种方式检索所选对象AdressBokPerson.

.net c# datagridview winforms

24
推荐指数
1
解决办法
3万
查看次数

好的DataGridView教程

我正在编写一个Windows C#应用程序,我希望使用数据网格视图,但无法找到一个好的教程来完全控制datagrid,例如:添加列,添加数据等.

datagridview winforms

23
推荐指数
1
解决办法
5万
查看次数

将文本添加到DataGridView行标题

C#允许您将一个String添加到DataGridView中的RowHeader吗?如果是这样,它是如何完成的?

我正在写一个Windows表单来显示到目前为止的年度客户付款数据.

ColumnHeaders显示1月,2月,3月等...而不是有一个DateTime.Now.Year的空白列我想把它放在RowHeader中,使其从实际的支付数据中脱颖而出.

.net c# datagridview winforms

23
推荐指数
5
解决办法
15万
查看次数

VB.NET:清除DataGridView

我试过了 -

DataGridView1.DataSource=Nothing
Run Code Online (Sandbox Code Playgroud)

DataGridView1.DataSource=Nothing
DataGridView1.Refresh()
Run Code Online (Sandbox Code Playgroud)

DataGridView1.RefreshEdit()
Run Code Online (Sandbox Code Playgroud)

它们都不起作用..

我编写了一个方法,在执行时设置DataGridView的DataSource.但每次执行它时,它都会使用新值复制数据并将其附加到DGV的先前内容中.我想清除内容然后添加值..这可能吗?

vb.net datasource datagridview clear datagridviewrow

23
推荐指数
3
解决办法
26万
查看次数

DataGridView验证和更改单元格值

我想在验证时操作DataGridView中的单元格,这样如果用户输入的值对数据库无效,但很容易转换为有效数据,程序会将值更改为合适的值.

我能够正确验证我的值,但当我尝试将其更改为有效的东西时,我得到了一个DataError.这是我的代码:

        private void unit_List_2_GroupsDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        Console.WriteLine("Validating");
        DataGridViewColumn col = this.unit_List_2_GroupsDataGridView.Columns[e.ColumnIndex];
        DataGridViewCell cell = this.unit_List_2_GroupsDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
        if (col == this.batchDataGridViewTextBoxColumn && this.unit_List_2_GroupsDataGridView.IsCurrentCellInEditMode)
        {
            Console.WriteLine("   Batch Column");
            DataRow[] rows = label_EntryDataSet.viewJobBatchList.Select(String.Format("Job={0} AND Display='{1}'"
                , comboBox1.SelectedValue, e.FormattedValue));
            if (rows.Length == 1)
            {
                Console.WriteLine("      Auto Completed item from list: {0}", rows[0]["Batch"]);
                //e.Cancel = true;
                cell.Value = rows[0]["Batch"];
                //this.unit_List_2_GroupsDataGridView.EndEdit();
            }
            else
            {
                Console.WriteLine("     No Autocomplete!");
                int i = 0;
                if (!int.TryParse(e.FormattedValue.ToString(), out i))
                {
                    Console.WriteLine("         Not an integer either");
                    e.Cancel = true; …
Run Code Online (Sandbox Code Playgroud)

c# datagridview validating

23
推荐指数
2
解决办法
4万
查看次数

c #datagridview使用FullRowSelect双击行

我的C#应用​​程序中有一个datagridview,用户应该只能点击完整的行.所以我将SelectionMode设置为FullRowSelect.

但是现在我想要一个在用户双击一行时触发的事件.我想在MessageBox中有行号.

我尝试了以下方法:

 this.roomDataGridView.CellContentDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.roomDataGridView_CellCont? ?entDoubleClick); 

 private void roomDataGridView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
 {
      MessageBox.Show(e.RowIndex.ToString());
 }
Run Code Online (Sandbox Code Playgroud)

不幸的是没有任何反应.我究竟做错了什么?

c# events datagridview double-click

23
推荐指数
3
解决办法
9万
查看次数

C#迭代DataGridView并更改行颜色

我有一个由多行和多列组成的datagridview.我想迭代每一行并检查特定列的内容.如果该列包含单词"NO",我想将整行的前景颜色更改为红色.到目前为止,这是对某些代码的尝试,但它肯定不起作用,开始怀疑我是否需要迭代每个单元格?

码:

foreach (DataGridViewRow dgvr in dataGridView1.Rows)
        {
            if (dgvr.Cells["FollowedUp"].Value.ToString() == ("No"))
            {
                dgvr.DefaultCellStyle.ForeColor = Color.Red;
            }
        }
Run Code Online (Sandbox Code Playgroud)

c# loops datagridview row winforms

22
推荐指数
3
解决办法
6万
查看次数

使用大量数据填充DataGridView的最佳方法

我有一个Windows窗体,有两个DataGridViews(DGV),每个将包含25,000多条记录和21列.我已经使用DataAdapter成功地从数据库加载了数据,然后我尝试使用for循环填充DGV.每种方法花费的时间大致相同.第一次将数据填入DGV时需要太长时间(7分钟以上),然后随后的时间更合理(~30秒).所以我的问题是,用平均<= 1分钟的大量数据加载DGV的最佳方法是什么?我非常喜欢DGV的功能,但是如果推动推动我愿意使用不同的技术,即使它意味着放弃一些功能.

c# datagridview

22
推荐指数
3
解决办法
5万
查看次数

DataGridView第一列,第一行,在Load上选中,我不想这样

所以基本上第一行中的第一列总是被选中,我无法找到一种方法来获得它,因此gridview没有选定的单元格.有帮助吗?

c# datagridview winforms

22
推荐指数
3
解决办法
4万
查看次数

在DataGridView中以编程方式选择行

我希望在某些事件之后选择之前选定行的行,我的代码如下所示.

int currentRow = dgvIcbSubsInfo.CurrentCell.RowIndex;
//code to execute
dgvIcbSubsInfo.Rows[currentRow].Selected = true;
Run Code Online (Sandbox Code Playgroud)

执行代码后,预览将如下所示.但我需要>在id = 1272741(蓝色选择)中获取符号,而不是在1272737中

在此输入图像描述

.net c# datagridview visual-studio-2010

22
推荐指数
1
解决办法
5万
查看次数