Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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行';将值从一个窗体转换为datagridview';这是另一张表格上的行吗?_C#_Winforms_Datagridview_Html Agility Pack - Fatal编程技术网

C#如何复制选定的datagridview行';将值从一个窗体转换为datagridview';这是另一张表格上的行吗?

C#如何复制选定的datagridview行';将值从一个窗体转换为datagridview';这是另一张表格上的行吗?,c#,winforms,datagridview,html-agility-pack,C#,Winforms,Datagridview,Html Agility Pack,我正在使用html agility pack获取网格视图的数据。我只想使用列按钮复制一行,将其发送到另一个表单上的空白网格视图。没有数据绑定或SQL。填充网格后,我将获得整个数据网格视图,而无需按下列按钮。我的密码是: 表格一 private void LeadsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { Database DB = new Database(LeadsDataGr

我正在使用html agility pack获取网格视图的数据。我只想使用列按钮复制一行,将其发送到另一个表单上的空白网格视图。没有数据绑定或SQL。填充网格后,我将获得整个数据网格视图,而无需按下列按钮。我的密码是:

表格一

private void LeadsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    Database DB = new Database(LeadsDataGridView.DataSource);

    var senderGrid = (DataGridView)sender;

    if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
    {
        DB.Row = LeadsDataGridView.CurrentRow;
    }

    Search_Table.AcceptChanges();

    LeadsDataGridView.DataSource = Search_Table;
}
表格二

public Database(object DataSource)
{
    InitializeComponent();
    InitDBTable();
    DB_GridView.DataSource = DataSource;
}

这里有一个很小的例子,希望能让你走上正轨

MainWindow.xaml

<Window x:Class="PassingValuesFromFormToForm_45425412.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PassingValuesFromFormToForm_45425412"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="61,60,0,0" VerticalAlignment="Top" Width="259"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="29,13,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Windows;

namespace PassingValuesFromFormToForm_45425412
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<dgvEntry> dgvList = new List<dgvEntry>();
        public MainWindow()
        {
            InitializeComponent();
            dgvList.Add(new PassingValuesFromFormToForm_45425412.dgvEntry { col1 = "blah blah", col2 = "blehbleh" });
            dataGrid.AutoGenerateColumns = true;
            dataGrid.ItemsSource = dgvList;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Window1 win1 = new Window1((dgvEntry)dataGrid.Items[0]);
            win1.Show();
        }
    }

    public class dgvEntry {
        public string col1 { get; set; }
        public string col2 { get; set; }
    }
}
使用System.Collections.Generic;
使用System.Windows;
命名空间传递值从FormToForm45425412
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
List dgvList=新列表();
公共主窗口()
{
初始化组件();
dgvList.Add(新的传递值来自表单45425412.dgvEntry{col1=“blah blah”,col2=“blehbleh”});
dataGrid.AutoGenerateColumns=true;
dataGrid.ItemsSource=dgvList;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
Window1 win1=newwindow1((dgvEntry)dataGrid.Items[0]);
win1.Show();
}
}
公开课{
公共字符串col1{get;set;}
公共字符串col2{get;set;}
}
}
Window1.xaml

<Window x:Class="PassingValuesFromFormToForm_45425412.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PassingValuesFromFormToForm_45425412"
        mc:Ignorable="d"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="23,39,0,0" VerticalAlignment="Top" Width="181"/>

    </Grid>
</Window>

Window1.xaml.cs

using System.Collections.Generic;
using System.Windows;

namespace PassingValuesFromFormToForm_45425412
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        public Window1(dgvEntry incomingItem)
        {
            InitializeComponent();
            dataGrid.AutoGenerateColumns = true;
            dataGrid.ItemsSource = new List<dgvEntry> { incomingItem };
        }
    }
}
使用System.Collections.Generic;
使用System.Windows;
命名空间传递值从FormToForm45425412
{
/// 
///Window1.xaml的交互逻辑
/// 
公共部分类Window1:Window
{
公共窗口1()
{
初始化组件();
}
公共窗口1(dgvEntry收入项目)
{
初始化组件();
dataGrid.AutoGenerateColumns=true;
dataGrid.ItemsSource=新列表{incomingItem};
}
}
}
这里是WinForms的更新。同样,这是一个最小的示例。就本示例而言,我没有在表单设计器中执行任何操作一切都是通过代码隐藏完成的

Form1.cs

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {

        DataGridView dgv = new DataGridView();
        BindingList<dgvitem> itemsList = new BindingList<dgvitem>();
        bool SendAsRow = true;//just a variable to trigger the proper method in 'Dgv_CellContentClick'
        Button independantButton = new Button();

        public Form1()
        {
            InitializeComponent();
            InitializeTheDGV();
            AddTheButton();
            itemsList.Add(new dgvitem { JustaTextField = "aksldjf sadfjasifuqw adsfasf" });
            itemsList.Add(new dgvitem { JustaTextField = "qwerioqu aisdfnvmz, oaa"});
        }

        private void InitializeTheDGV()
        {
            dgv.Location = new Point(this.Location.X + 5, this.Location.Y + 5);
            dgv.DataSource = itemsList;
            dgv.AutoGenerateColumns = false;
            this.Controls.Add(dgv);
            dgv.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "My col header", Name="mycol1" });
            dgv.Columns.Add(new DataGridViewButtonColumn() { HeaderText = "click in this column", Name = "mycol2" });
            dgv.Columns["mycol1"].DataPropertyName = "JustaTextField";
            dgv.CellContentClick += Dgv_CellContentClick;
        }

        private void Dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!(sender is DataGridView))
            {
                return;
            }

            /*
             * Experiment and Pick your poison
             */
            if (SendAsRow)
            {
                Form2 f2r = new Form2(dgv.Rows[e.RowIndex]);
                f2r.Show();
            }
            else
            {
                Form2 f2 = new Form2((string)dgv.Rows[e.RowIndex].Cells[0].FormattedValue);
                f2.Show();
            }
            /**/
        }


        private void AddTheButton()
        {
            independantButton.Location = new Point(this.Location.X + 5, this.Location.Y + dgv2.Height + 15);
            independantButton.Click += IndependantButton_Click;
            this.Controls.Add(independantButton);
        }

        private void IndependantButton_Click(object sender, System.EventArgs e)
        {
            /*
             * Experiment and Pick your poison
             */
            if (SendAsRow)
            {
                Form2 f2r = new Form2(dgv.SelectedRows[0]);
                f2r.Show();
            }
            else
            {
                Form2 f2 = new Form2((string)dgv.SelectedRows[0].Cells[0].FormattedValue);
                f2.Show();
            }
            /**/

        }
    }


    public class dgvitem
    {
        public string JustaTextField { get; set; }
    }
}
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        DataGridView dgv2 = new DataGridView();
        BindingList<dgv2item> dgv2list = new BindingList<dgv2item>();

        //this is the 'default' constructor which takes no argument
        public Form2()
        {
            InitializeComponent();
            MakeTheGrid();
        }

        //this form constructor takes a String parameter so you can pass only a string
        public Form2(string incomingText)
        {
            InitializeComponent();
            MakeTheGrid();
            dgv2list.Add(new dgv2item { coolBeans = incomingText });//add the incoming String to the itemList, which will in-turn update the DataGridView
        }


        //this form constructor takes a DataGridViewRow parameter so you can pass the whole row
        public Form2(DataGridViewRow incomingRow)
        {
            InitializeComponent();
            MakeTheGrid();
            dgv2list.Add(new dgv2item { coolBeans = (string)incomingRow.Cells[0].FormattedValue});//add the value of the cell you want out of the row to the itemlist, which will in-turn update the DataGridView
        }


        private void MakeTheGrid()
        {
            dgv2.Location = new Point(this.Location.X + 15, this.Location.Y + 15);//it has to go somewhere...
            dgv2.AutoGenerateColumns = true;
            dgv2.DataSource = dgv2list;//define where to find the data
            this.Controls.Add(dgv2);//add it to the form
        }
    }


    public class dgv2item
    {
        public string coolBeans { get; set; }
    }
}
使用System.ComponentModel;
使用系统图;
使用System.Windows.Forms;
命名空间Windows窗体应用程序2
{
公共部分类Form1:Form
{
DataGridView dgv=新建DataGridView();
BindingList itemsList=新建BindingList();
bool SendAsRow=true;//只是一个变量,用于触发“Dgv_CellContentClick”中的正确方法
按钮独立按钮=新按钮();
公共表格1()
{
初始化组件();
初始化ethedgv();
添加按钮();
添加(新的dgvitem{JustaTextField=“aksldjf sadfjasifuqw adsfasf”});
添加(新的dgvitem{JustaTextField=“qwerioqu aisdfnvmz,oaa”});
}
私有void初始化ethedgv()
{
dgv.Location=新点(this.Location.X+5,this.Location.Y+5);
dgv.DataSource=itemsList;
dgv.AutoGenerateColumns=false;
this.Controls.Add(dgv);
添加(新的DataGridViewTextBoxColumn(){HeaderText=“My col header”,Name=“mycol1”});
添加(新的DataGridViewButtonColumn(){HeaderText=“单击此列”,Name=“mycl2”});
dgv.Columns[“mycl1”].DataPropertyName=“JustaTextField”;
dgv.CellContentClick+=dgv_CellContentClick;
}
私有void Dgv_CellContentClick(对象发送方,DataGridViewCellEventArgs e)
{
如果(!(发送方为DataGridView))
{
返回;
}
/*
*尝试并选择你的毒药
*/
if(SendAsRow)
{
Form2 f2r=新的Form2(dgv.Rows[e.RowIndex]);
f2r.Show();
}
其他的
{
Form2 f2=新Form2((字符串)dgv.Rows[e.RowIndex]。单元格[0]。FormattedValue);
f2.Show();
}
/**/
}
私有void AddTheButton()
{
独立按钮.Location=新点(this.Location.X+5,this.Location.Y+dgv2.Height+15);
独立按钮。单击+=独立按钮\u单击;
this.Controls.Add(独立按钮);
}
私有void独立按钮(对象发送者,System.EventArgs e)
{
/*
*尝试并选择你的毒药
*/
if(SendAsRow)
{
Form2 f2r=新Form2(dgv.SelectedRows[0]);
f2r.Show();
}
其他的
{
Form2 f2=新Form2((字符串)dgv.SelectedRows[0]。单元格[0]。FormattedValue);
f2.Show();
}
/**/
}
}
公共类dgvitem
{
公共字符串JustaTextField{get;set;}
}
}
Form2.cs

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {

        DataGridView dgv = new DataGridView();
        BindingList<dgvitem> itemsList = new BindingList<dgvitem>();
        bool SendAsRow = true;//just a variable to trigger the proper method in 'Dgv_CellContentClick'
        Button independantButton = new Button();

        public Form1()
        {
            InitializeComponent();
            InitializeTheDGV();
            AddTheButton();
            itemsList.Add(new dgvitem { JustaTextField = "aksldjf sadfjasifuqw adsfasf" });
            itemsList.Add(new dgvitem { JustaTextField = "qwerioqu aisdfnvmz, oaa"});
        }

        private void InitializeTheDGV()
        {
            dgv.Location = new Point(this.Location.X + 5, this.Location.Y + 5);
            dgv.DataSource = itemsList;
            dgv.AutoGenerateColumns = false;
            this.Controls.Add(dgv);
            dgv.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "My col header", Name="mycol1" });
            dgv.Columns.Add(new DataGridViewButtonColumn() { HeaderText = "click in this column", Name = "mycol2" });
            dgv.Columns["mycol1"].DataPropertyName = "JustaTextField";
            dgv.CellContentClick += Dgv_CellContentClick;
        }

        private void Dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!(sender is DataGridView))
            {
                return;
            }

            /*
             * Experiment and Pick your poison
             */
            if (SendAsRow)
            {
                Form2 f2r = new Form2(dgv.Rows[e.RowIndex]);
                f2r.Show();
            }
            else
            {
                Form2 f2 = new Form2((string)dgv.Rows[e.RowIndex].Cells[0].FormattedValue);
                f2.Show();
            }
            /**/
        }


        private void AddTheButton()
        {
            independantButton.Location = new Point(this.Location.X + 5, this.Location.Y + dgv2.Height + 15);
            independantButton.Click += IndependantButton_Click;
            this.Controls.Add(independantButton);
        }

        private void IndependantButton_Click(object sender, System.EventArgs e)
        {
            /*
             * Experiment and Pick your poison
             */
            if (SendAsRow)
            {
                Form2 f2r = new Form2(dgv.SelectedRows[0]);
                f2r.Show();
            }
            else
            {
                Form2 f2 = new Form2((string)dgv.SelectedRows[0].Cells[0].FormattedValue);
                f2.Show();
            }
            /**/

        }
    }


    public class dgvitem
    {
        public string JustaTextField { get; set; }
    }
}
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        DataGridView dgv2 = new DataGridView();
        BindingList<dgv2item> dgv2list = new BindingList<dgv2item>();

        //this is the 'default' constructor which takes no argument
        public Form2()
        {
            InitializeComponent();
            MakeTheGrid();
        }

        //this form constructor takes a String parameter so you can pass only a string
        public Form2(string incomingText)
        {
            InitializeComponent();
            MakeTheGrid();
            dgv2list.Add(new dgv2item { coolBeans = incomingText });//add the incoming String to the itemList, which will in-turn update the DataGridView
        }


        //this form constructor takes a DataGridViewRow parameter so you can pass the whole row
        public Form2(DataGridViewRow incomingRow)
        {
            InitializeComponent();
            MakeTheGrid();
            dgv2list.Add(new dgv2item { coolBeans = (string)incomingRow.Cells[0].FormattedValue});//add the value of the cell you want out of the row to the itemlist, which will in-turn update the DataGridView
        }


        private void MakeTheGrid()
        {
            dgv2.Location = new Point(this.Location.X + 15, this.Location.Y + 15);//it has to go somewhere...
            dgv2.AutoGenerateColumns = true;
            dgv2.DataSource = dgv2list;//define where to find the data
            this.Controls.Add(dgv2);//add it to the form
        }
    }


    public class dgv2item
    {
        public string coolBeans { get; set; }
    }
}
使用System.ComponentModel;
使用系统图;
使用System.Windows.Forms;
命名空间Windows窗体应用程序2
{
公共部分类表单2:表单
{
DataGridView dgv2=新建DataGridView();
BindingList dgv2list=新建BindingList();
//这是不带参数的“默认”构造函数
公共表格2()
{
初始化组件();
生成网格();
}
//此表单构造函数接受字符串参数,因此您只能传递字符串
公共表单2(字符串输入文本)
{
初始化组件();
生成网格();
dgv2list.Add(新的dgv2item{coolBeans=incomingText});//将传入字符串添加到itemList,这将更新