Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.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卡在按钮1#U单击事件中?_C#_Datagridview - Fatal编程技术网

C#DataGridView卡在按钮1#U单击事件中?

C#DataGridView卡在按钮1#U单击事件中?,c#,datagridview,C#,Datagridview,我试图从DataGridView中“Document Number”列中的选定单元格中获取字符串形式的单元格值。我从SharePoint检索数据并填充DataGridView的代码运行良好,但我似乎陷入了一个循环中?填充DataGridView后,我无法执行任何其他方法。我可以选择一个新的搜索词并再次成功执行Button1_Click事件,但我无法执行任何其他方法 using System; using System.Collections.Generic; using System.Compo

我试图从DataGridView中“Document Number”列中的选定单元格中获取字符串形式的单元格值。我从SharePoint检索数据并填充DataGridView的代码运行良好,但我似乎陷入了一个循环中?填充DataGridView后,我无法执行任何其他方法。我可以选择一个新的搜索词并再次成功执行Button1_Click事件,但我无法执行任何其他方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace BuyersForeverFriend
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string userSearch = textBox1.Text;
            button1.Enabled = true;




        }

        private void button1_Click(object sender, EventArgs e)
        {

            using (ClientContext ctx = new ClientContext("https://teamsites.nafta.fcagroup.com/sites/PSOAPPS/iws/"))
            {
                var userInput = textBox1.Text;
                Web web = ctx.Web;
                List list = web.Lists.GetById(new Guid("61ef6657-eff7-42cb-99e1-8afd590334ec"));
                var q = new CamlQuery() { ViewXml = "<View><Query><Where><Contains><FieldRef Name='Item_x0020_Description' /><Value Type='Note'>" + userInput + "</Value></Contains></Where></Query><ViewFields><FieldRef Name='Title' /><FieldRef Name='Topic_x002d_' /><FieldRef Name='Item_x0020_Description' /></ViewFields><QueryOptions /></View>" };
                var r = list.GetItems(q);
                ctx.Load(r);
                ctx.ExecuteQuery();

                if (r.Count != 0)
                {
                    var searchResults = new DataTable();

                    searchResults.Columns.AddRange(new[]
                    {
                    new DataColumn("ID"), new DataColumn("Document Number"), new DataColumn("Item Description"), new DataColumn("Topic")});

                    foreach (var oListItem in r)
                    {
                        searchResults.Rows.Add(oListItem["ID"], oListItem["Title"], oListItem["Item_x0020_Description"],
                            oListItem["Topic_x002d_"]);
                    }

                    if (dataGridView1 != null)
                    {
                        dataGridView1.DataSource = searchResults;
                        dataGridView1.Refresh();
                        dataGridView1.Columns[2].DefaultCellStyle.Format = "dd'/'MM'/'yyyy";
                        var dataGridViewColumn = dataGridView1.Columns["ID"];
                        if (dataGridViewColumn != null)
                            dataGridViewColumn.Visible = false;
                    }
                    else 
                        MessageBox.Show("after else statement");
                }

                
            }
            MessageBox.Show("after sharepoint if statement");
            return;

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            MessageBox.Show("a cell was clicked");

        }

        
        private void dataGridView1_SelectionChanged(object sender, EventArgs e) 
        {

            MessageBox.Show("starting routine");
            if (dataGridView1.SelectedCells.Count > 0)
            {
                int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
                DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];
                string a = Convert.ToString(selectedRow.Cells["Document Number"].Value);
                MessageBox.Show(a);
               
                textBox2.Text = ("The var =" + a);
                
            }
            else MessageBox.Show("if did not end up true");
        }


    }
}

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用Microsoft.SharePoint.Client;
使用SP=Microsoft.SharePoint.Client;
命名空间购买者永远的朋友
{
公共部分类Form1:System.Windows.Forms.Form
{
公共表格1()
{
初始化组件();
}
私有void textBox1\u TextChanged(对象发送方,事件参数e)
{
字符串userSearch=textBox1.Text;
按钮1.启用=真;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
使用(ClientContext ctx=newclientcontext(“https://teamsites.nafta.fcagroup.com/sites/PSOAPPS/iws/"))
{
var userInput=textBox1.Text;
Web=ctx.Web;
List List=web.Lists.GetById(新Guid(“61ef6657-eff7-42cb-99e1-8AFD59034EC”);
var q=new CamlQuery(){ViewXml=”“+userInput+“”};
var r=list.GetItems(q);
载重量(r);
ctx.ExecuteQuery();
如果(r.Count!=0)
{
var searchResults=新数据表();
searchResults.Columns.AddRange(新[]
{
新数据列(“ID”)、新数据列(“文件编号”)、新数据列(“项目说明”)、新数据列(“主题”)};
foreach(r中的var oListItem)
{
searchResults.Rows.Add(oListItem[“ID”]、oListItem[“Title”]、oListItem[“Item_x0020_Description”],
oListItem[“主题”];
}
如果(dataGridView1!=null)
{
dataGridView1.DataSource=搜索结果;
dataGridView1.Refresh();
dataGridView1.Columns[2].DefaultCellStyle.Format=“dd'/'MM'/'yyyy”;
var dataGridViewColumn=dataGridView1.Columns[“ID”];
如果(dataGridViewColumn!=null)
dataGridViewColumn.Visible=false;
}
其他的
MessageBox.Show(“在else语句之后”);
}
}
MessageBox.Show(“在sharepoint if语句之后”);
返回;
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有void dataGridView1\u CellContentClick(对象发送者,DataGridViewCellEventArgs e)
{
MessageBox.Show(“单击了一个单元格”);
}
private void dataGridView1\u SelectionChanged(对象发送方,事件参数e)
{
MessageBox.Show(“启动例程”);
如果(dataGridView1.SelectedCells.Count>0)
{
int selectedrowindex=dataGridView1.SelectedCells[0]。行索引;
DataGridViewRow selectedRow=dataGridView1.Rows[selectedrowindex];
字符串a=Convert.ToString(selectedRow.Cells[“文档编号”].Value);
MessageBox.Show(a);
textBox2.Text=(“var=”+a);
}
else MessageBox.Show(“如果结果不是真的”);
}
}
}

在将它们添加到表单方法后,我未能初始化它们…poof everything works!:

        public Form1()
        {
            InitializeComponent();

            dataGridView1.CellClick += dataGridView1_CellClick;
            dataGridView1.SelectionChanged += dataGridView1_SelectionChanged;
        }
你最好的朋友,那个,应该帮助你!