使用datagridviewbutton打开文件夹 [英] Open the folder using datagridviewbutton

查看:100
本文介绍了使用datagridviewbutton打开文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用datagridviewbuttonIMAGE打开一个文件夹,文件夹名称是rowindex [0],这里是MR_NO。

有六列名称为MR_NO,NAME, GENDER,DOB,PATH,IMAGE。

这里IMAGE列是DataGridViewButton。我在这里没有得到任何价值或消息​​,我不知道我错在哪里。



这里的问题是我无法使用DatagridViewbutton'IMAGE'并使用按钮中的MR_NO列值打开文件夹

剩余的东西是正确的。



我尝试过:



我使用了以下代码。

代码:

I want to open a folder using datagridviewbutton "IMAGE" and the folder name is rowindex[0], here it is "MR_NO".
there are six column with names "MR_NO,NAME,GENDER,DOB,PATH,IMAGE".
Here "IMAGE" column is DataGridViewButton. I am not getting any value or messages here,I don't know where I am wrong.

"Here problem is that I am unable to use DatagridViewbutton 'IMAGE' and use MR_NO column value in the button to open a folder"
Remaining things are correct.

What I have tried:

I used the following code.
CODE:

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

namespace Autoupload
{
    public partial class Form1 : Form
    {
        SqlConnection cn = new SqlConnection("Integrated Security=true;Initial Catalog=hospital;server=localhost\\sqlexpress");
        public Form1()
        {
            InitializeComponent();
            load();
        }
        void load()
        {
            cn.Open();
            SqlCommand cmddatabase = new SqlCommand("GetCustomers", cn);
            try
            {
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = cmddatabase;
                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                BindingSource bsource = new BindingSource();
                bsource.DataSource = dbdataset;
                dataGridView1.DataSource = bsource;
                sda.Update(dbdataset);
          /*  DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(btn);
            btn.HeaderText = "IMAGES";
            btn.Text = "Click Here";
            btn.Name = "btn";
            btn.UseColumnTextForButtonValue = true;*/


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
      /*  private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dataGridView1.Columns[e.ColumnIndex].Name == "IMAGE")
            {
                string mrnum=dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
               System.Diagnostics.Process.Start("D:\\ "+mrnum);
            }
        
        }*/

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'hospitalDataSet.GetCustomers' table. You can move, or remove it, as needed.
            this.getCustomersTableAdapter.Fill(this.hospitalDataSet.GetCustomers);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "IMAGE")
            {
                  string mrnum=dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                  System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
            {
                FileName ="D:\\ "+mrnum,
                UseShellExecute = true,
                Verb = "open"
            });
            }
        }
    }

推荐答案

我们无法告诉你该怎么做 - 我们无法访问您的数据!

所以从调试器开始,并在该行上放置一个断点:

We can't tell you what to do - we don't have access to your data!
So start with the debugger, and put a breakpoint on the line:
System.Diagnostics.Process.Start("D:\\ "+mrnum);

运行你的应用程序,当它到达该行时,它将停止。 Visual Studio将等待您告诉它该做什么。

使用调试器查看 mrnum 中的值并精神上添加 D:\部分形成一个完整的字符串。查看您的文件系统,看看您获得的文件夹是否确实存在。字符串必须是完整,正确,有效的文件规范,否则它将失败。值得这样做:

Run your app, and when it reaches the line, it will stop. Visual Studio will then wait for you to tell it what to do.
Use the debugger to look at the value in mrnum and mentally add the "D:\" part to form a full string. Look at your file system, and see if the folder you get as a result actually exists. The string has to be a full, proper, valid file specification or it will just fail. It's worth doing this via:

string path = Path.Combine("D:\\ ", mrnum);
System.Diagnostics.Process.Start(path);

所以你可以看到究竟发生了什么。

但是......我怀疑它是那个空间字符串,我自己...

我讨厌标记![/ edit]

so you can see exactly what is going on.
But... I suspect it's that space in the string, myself...
[edit]I hate markup![/edit]


如果要打开文件夹并选择,可以使用此选项一个文件:

You could use this if you want to open the folder and select a file:
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\MR_NO" ;



请参阅: OpenFileDialog类(System.Windows.Forms) [ ^ ]



如果您只想打开文件夹,请使用此选项:


See: OpenFileDialog Class (System.Windows.Forms)[^]

Use this if you only want to open a folder:

private FolderBrowserDialog folderBrowserDialog1;
private OpenFileDialog openFileDialog1;



请参阅:FolderBrowserDialog类(System.Windows.Forms) [ ^ ]



如果您想要打开资源管理器,请使用此选项:


See: FolderBrowserDialog Class (System.Windows.Forms)[^]

Or use this if you want Explorer to open:

System.Diagnostics.Process.Start("Explorer.exe", @"/select,""" + FilePath + "\"")


啊哈,我想我现在明白你的问题,试试这个:

Aha, I think I now understand your problem, try this:
// Add CellContentClick event to your DataGridView first in Designer
// and remove other possible conflicting event handlers.
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == "IMAGE")
    {
        //  your code...
        var str = myDataGridView.CurrentRow.Cells[0].Value.ToString();
        MessageBox.Show("You Have Selected " + str);
    }
}


这篇关于使用datagridviewbutton打开文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆