Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用dataGridView时出错_C#_Datagridview - Fatal编程技术网

C# 使用dataGridView时出错

C# 使用dataGridView时出错,c#,datagridview,C#,Datagridview,我必须将dataGridView1显示为 SN Name Subject Topic Subtopic 1. Mr.SK Jha Physics Optics Diffraction Interference Mechanics

我必须将
dataGridView1
显示为

SN    Name        Subject       Topic            Subtopic
1.    Mr.SK Jha   Physics       Optics           Diffraction
                                                 Interference
                                Mechanics        MKS
                                Electromagnetic
2.    Mr.XYZ     Chemistry      Inorganic        Ethene
这里的主题与subject_id相同,它可能有许多未指定为固定的数据

我见过很多,但它给出了
GridViewRow
作为错误


我使用的是Visual Studio 2013 framework 4.5,最简单的方法是将数据源设置为datatable

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("SN", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Subject", typeof(string));
            dt.Columns.Add("Topic", typeof(string));
            dt.Columns.Add("Subtopic", typeof(string));

            dt.Rows.Add(new object[] { "1.", "Mr.SK Jha", "Physics", "Optics", "Diffraction Interference" });
            dt.Rows.Add(new object[] { "", "", "", "Mechanics", "MKS" });
            dt.Rows.Add(new object[] { "", "", "", "Electromagnetic" });
            dt.Rows.Add(new object[] { 2, "Mr.XYZ", "Chemistry", "Inorganic", "Ethene" });

            dataGridView1.DataSource = dt;
        }
    }
}
​

最简单的方法是将数据源设置为datatable

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("SN", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Subject", typeof(string));
            dt.Columns.Add("Topic", typeof(string));
            dt.Columns.Add("Subtopic", typeof(string));

            dt.Rows.Add(new object[] { "1.", "Mr.SK Jha", "Physics", "Optics", "Diffraction Interference" });
            dt.Rows.Add(new object[] { "", "", "", "Mechanics", "MKS" });
            dt.Rows.Add(new object[] { "", "", "", "Electromagnetic" });
            dt.Rows.Add(new object[] { 2, "Mr.XYZ", "Chemistry", "Inorganic", "Ethene" });

            dataGridView1.DataSource = dt;
        }
    }
}
​

最简单的方法是将数据源设置为datatable

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("SN", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Subject", typeof(string));
            dt.Columns.Add("Topic", typeof(string));
            dt.Columns.Add("Subtopic", typeof(string));

            dt.Rows.Add(new object[] { "1.", "Mr.SK Jha", "Physics", "Optics", "Diffraction Interference" });
            dt.Rows.Add(new object[] { "", "", "", "Mechanics", "MKS" });
            dt.Rows.Add(new object[] { "", "", "", "Electromagnetic" });
            dt.Rows.Add(new object[] { 2, "Mr.XYZ", "Chemistry", "Inorganic", "Ethene" });

            dataGridView1.DataSource = dt;
        }
    }
}
​

最简单的方法是将数据源设置为datatable

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("SN", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Subject", typeof(string));
            dt.Columns.Add("Topic", typeof(string));
            dt.Columns.Add("Subtopic", typeof(string));

            dt.Rows.Add(new object[] { "1.", "Mr.SK Jha", "Physics", "Optics", "Diffraction Interference" });
            dt.Rows.Add(new object[] { "", "", "", "Mechanics", "MKS" });
            dt.Rows.Add(new object[] { "", "", "", "Electromagnetic" });
            dt.Rows.Add(new object[] { 2, "Mr.XYZ", "Chemistry", "Inorganic", "Ethene" });

            dataGridView1.DataSource = dt;
        }
    }
}
​

我假设您希望DGV看起来像一个分组的表,并抑制重复值

这是一个将重复单元格绘制为透明的例程:

static void PaintGrouped(DataGridView dgv)
{
    if (dgv.Rows.Count < 2) return;
    if (dgv.Columns.Count < 2) return;
    for (int row = 1; row < dgv.Rows.Count; row++)
    {
        bool suppressing = dgv[0, row].Value.Equals(dgv[0, row - 1].Value);
        for (int col = 1; col < dgv.Columns.Count; col++)
        {
            bool equal = dgv[col, row].Value.Equals(dgv[col, row - 1].Value);
            suppressing = suppressing && equal;
            dgv[col, row].Style.ForeColor = supressing ? Color.Transparent : Color.Black;
        }
    }
}
static void PaintGrouped(DataGridView dgv)
{
如果(dgv.Rows.Count<2)返回;
if(dgv.Columns.Count<2)返回;
对于(int row=1;row

请注意,所有值仍在原位,可以更改或复制。在任何更改之后,您应该重新应用该例程!还请注意,我已决定从不抑制第1列。

我假设您希望DGV看起来像一个分组表,并且抑制重复值

这是一个将重复单元格绘制为透明的例程:

static void PaintGrouped(DataGridView dgv)
{
    if (dgv.Rows.Count < 2) return;
    if (dgv.Columns.Count < 2) return;
    for (int row = 1; row < dgv.Rows.Count; row++)
    {
        bool suppressing = dgv[0, row].Value.Equals(dgv[0, row - 1].Value);
        for (int col = 1; col < dgv.Columns.Count; col++)
        {
            bool equal = dgv[col, row].Value.Equals(dgv[col, row - 1].Value);
            suppressing = suppressing && equal;
            dgv[col, row].Style.ForeColor = supressing ? Color.Transparent : Color.Black;
        }
    }
}
static void PaintGrouped(DataGridView dgv)
{
如果(dgv.Rows.Count<2)返回;
if(dgv.Columns.Count<2)返回;
对于(int row=1;row

请注意,所有值仍在原位,可以更改或复制。在任何更改之后,您应该重新应用该例程!还请注意,我已决定从不抑制第1列。

我假设您希望DGV看起来像一个分组表,并且抑制重复值

这是一个将重复单元格绘制为透明的例程:

static void PaintGrouped(DataGridView dgv)
{
    if (dgv.Rows.Count < 2) return;
    if (dgv.Columns.Count < 2) return;
    for (int row = 1; row < dgv.Rows.Count; row++)
    {
        bool suppressing = dgv[0, row].Value.Equals(dgv[0, row - 1].Value);
        for (int col = 1; col < dgv.Columns.Count; col++)
        {
            bool equal = dgv[col, row].Value.Equals(dgv[col, row - 1].Value);
            suppressing = suppressing && equal;
            dgv[col, row].Style.ForeColor = supressing ? Color.Transparent : Color.Black;
        }
    }
}
static void PaintGrouped(DataGridView dgv)
{
如果(dgv.Rows.Count<2)返回;
if(dgv.Columns.Count<2)返回;
对于(int row=1;row

请注意,所有值仍在原位,可以更改或复制。在任何更改之后,您应该重新应用该例程!还请注意,我已决定从不抑制第1列。

我假设您希望DGV看起来像一个分组表,并且抑制重复值

这是一个将重复单元格绘制为透明的例程:

static void PaintGrouped(DataGridView dgv)
{
    if (dgv.Rows.Count < 2) return;
    if (dgv.Columns.Count < 2) return;
    for (int row = 1; row < dgv.Rows.Count; row++)
    {
        bool suppressing = dgv[0, row].Value.Equals(dgv[0, row - 1].Value);
        for (int col = 1; col < dgv.Columns.Count; col++)
        {
            bool equal = dgv[col, row].Value.Equals(dgv[col, row - 1].Value);
            suppressing = suppressing && equal;
            dgv[col, row].Style.ForeColor = supressing ? Color.Transparent : Color.Black;
        }
    }
}
static void PaintGrouped(DataGridView dgv)
{
如果(dgv.Rows.Count<2)返回;
if(dgv.Columns.Count<2)返回;
对于(int row=1;row


请注意,所有值仍在原位,可以更改或复制。在任何更改之后,您应该重新应用该例程!还要注意的是,我决定永远不要取消第1列。

该示例的可能重复项是ASP。你在做Winforms,对吗?(请添加标记!)-DataGridView不支持分组。只有办法让他们看起来像他们一样。。一种方法是在重复单元格中使用透明文本。您是否解决了问题?示例的可能副本是ASP。你在做Winforms,对吗?(请添加标记!)-DataGridView不支持分组。只有办法让他们看起来像他们一样。。一种方法是在重复单元格中使用透明文本。您是否解决了问题?示例的可能副本是ASP。你在做Winforms,对吗?(请添加标记!)-DataGridView不支持分组。只有办法让他们看起来像他们一样。。一种方法是在重复单元格中使用透明文本。您是否解决了问题?示例的可能副本是ASP。你在做Winforms,对吗?(请添加标记!)-DataGridView不支持分组。只有办法让他们看起来像他们一样。。一种是在重复单元格中使用透明文本。您解决问题了吗?这将不再允许您访问单个单元格。TaW:请解释您的问题。您可以访问datatable或datagridview中的单元格。真的吗?您确定可以分别访问“光学衍射干涉”这三个值吗?我想这应该是可能的,但我可能错了1.看看列名。不清楚哪些字符串进入哪些列,以及是否有单元格为空。不要责怪输入定义不明确的解决方案。解决方案是好的。同意,问题不是100%清楚,你的解决方案是可靠的,至少如果它是应用程序的话