none
Host Browse buttonControls in Windows Forms DataGridView Cells RRS feed

  • Question

  • Hi,

    I need to add browse button control in DataGridViewTextBoxColumn at the end of the cell (as a icon). if user click on button control, folderBrowserControl dialog should be open, user can select folder. The user selected folder path should be displayed in DataGridViewTextBoxColumn (path should be displayed in TextBoxColumn where browse button is palaced.

     

    similar to calendar control : http://msdn.microsoft.com/en-au/library/7tas5c80.aspx

    Please help me out.

     


    • Edited by sanjuG Tuesday, November 8, 2011 9:51 AM
    Tuesday, November 8, 2011 9:48 AM

Answers

  • Hi sanjug,

    You can also customize your dgv by creating a textboxbutton column in it. Try also this sample:

    public partial class Form1 : Form
        {
            TextBoxButtonControl txtbtnControl;
            DataGridViewTextBoxColumn Code, Description;
    
            public Form1()
            {
                InitializeComponent();
    
                this.Code = new DataGridViewTextBoxColumn();
                this.Code.Name = "FilePath";
                this.Code.Width = 300;
                this.Code.Resizable = DataGridViewTriState.False;
                this.dataGridView1.Columns.Add(Code);
    
                this.Description = new DataGridViewTextBoxColumn();
                this.Description.Name = "Description";
                this.Description.Width = 150;
                this.dataGridView1.Columns.Add(Description);
    
                //adding textboxbutton control at code column...
                this.txtbtnControl = new TextBoxButtonControl();
                this.txtbtnControl.Visible = false;
                this.dataGridView1.Controls.Add(this.txtbtnControl);
    
                this.txtbtnControl.btnCode.Click += new EventHandler(btnCode_Click);
                this.txtbtnControl.Leave += new EventHandler(txtbtnControl_Leave);
                this.txtbtnControl.txtCode.TextChanged += new EventHandler(txtCode_TextChanged);
    
                this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
                this.dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);
                this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
            }
    
            private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                this.txtbtnControl.Visible = false;
            }
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == Code.Index)
                {
                    try
                    {
                        Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
                        this.txtbtnControl.Location = rect.Location;
                        this.txtbtnControl.Size = rect.Size;
                        this.txtbtnControl.btnCode.Text = "..";
                        this.txtbtnControl.Visible = true;
                        this.txtbtnControl.Focus();
                        if (!string.IsNullOrEmpty(this.dataGridView1.CurrentRow.Cells[Code.Name].Value.ToString()))
                        {
                            this.txtbtnControl.txtCode.Text = this.dataGridView1.CurrentRow.Cells[Code.Name].Value.ToString();
                        }
                        else
                        {
                            this.txtbtnControl.txtCode.Text = "";
                        }
                    }
                    catch (Exception) { }
                }
            }
    
            private void txtCode_TextChanged(object sender, EventArgs e)
            {
                if (this.txtbtnControl.txtCode.Modified)
                {
                    this.dataGridView1.CurrentRow.Cells[Code.Name].Value = this.txtbtnControl.txtCode.Text;
                }
            }
    
            private void txtbtnControl_Leave(object sender, EventArgs e)
            {
                try
                {
                    this.txtbtnControl.Visible = false;
                    this.txtbtnControl.txtCode.Text = "";
                }
                catch (Exception)
                {
                }
            }
    
            private void btnCode_Click(object sender, EventArgs e)
            {
                //Form2 form2 = new Form2();
                //form2.ShowDialog();
                OpenFileDialog dialog = new OpenFileDialog();
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value = dialog.FileName.ToString();
                        txtbtnControl.txtCode.Text = dialog.FileName.ToString();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
    
        class TextBoxButtonControl : UserControl
        {
            public TextBox txtCode;
            public Button btnCode;
    
            public TextBoxButtonControl()
            {
                this.txtCode = new TextBox();
                this.Controls.Add(this.txtCode);
                this.btnCode = new Button();
                this.Controls.Add(this.btnCode);
                this.renderControl();
            }
            public void renderControl()
            {
                this.txtCode.Location = new Point(0, 0);
                //this.txtCode.Width = 2 * this.Width / 3 - 17;
                this.txtCode.Width = this.Width + 115;
                this.txtCode.Height = this.Height;
                this.btnCode.Location = new Point(this.Width + 115, 0);
                this.btnCode.Width = 32;
                this.btnCode.Height = 21;
            }
        }
    
    Hope it helps

    • Marked as answer by Bob Wu-MT Friday, November 25, 2011 2:48 AM
    Thursday, November 10, 2011 10:22 AM

All replies

  • Hi SanjuG,

    I think you can use DataGridViewTextBoxColumn and DataGridViewButtonColum. DataGridViewTextBoxColumn is used to save the path and DataGridViewButtonColum is use to click.

    To archive this, check the following code:

    private void Form1_Load(object sender, EventArgs e)

    {

        DataGridViewTextBoxColumn textBoxCol = new DataGridViewTextBoxColumn();

        DataGridViewButtonColumn buttonCol = new DataGridViewButtonColumn();

        this.dataGridView1.Columns.Add(textBoxCol);

        this.dataGridView1.Columns.Add(buttonCol);

     

        this.dataGridView1.RowCount = 4;

        dataGridView1.CellClick +=

            new DataGridViewCellEventHandler(dataGridView1_CellClick);

    }

     

    void dataGridView1_CellClick(object sender,

        DataGridViewCellEventArgs e)

    {

        if (dataGridView1.Columns[e.ColumnIndex].GetType().Equals(typeof(DataGridViewButtonColumn)))

        {

            OpenFileDialog dialog = new OpenFileDialog();

            if (dialog.ShowDialog() == DialogResult.OK)

            {

                try

                {

                    dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex-1].Value = dialog.FileName.ToString();

                }

                catch(Exception ex)

                {

                }

            }

        }

    }

    Best Regards,

     


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Hardz Tarrayo Thursday, November 10, 2011 10:00 AM
    • Marked as answer by Bob Wu-MT Friday, November 25, 2011 2:48 AM
    • Unmarked as answer by Bob Wu-MT Monday, November 28, 2011 9:12 AM
    Thursday, November 10, 2011 9:29 AM
  • Hi sanjug,

    You can also customize your dgv by creating a textboxbutton column in it. Try also this sample:

    public partial class Form1 : Form
        {
            TextBoxButtonControl txtbtnControl;
            DataGridViewTextBoxColumn Code, Description;
    
            public Form1()
            {
                InitializeComponent();
    
                this.Code = new DataGridViewTextBoxColumn();
                this.Code.Name = "FilePath";
                this.Code.Width = 300;
                this.Code.Resizable = DataGridViewTriState.False;
                this.dataGridView1.Columns.Add(Code);
    
                this.Description = new DataGridViewTextBoxColumn();
                this.Description.Name = "Description";
                this.Description.Width = 150;
                this.dataGridView1.Columns.Add(Description);
    
                //adding textboxbutton control at code column...
                this.txtbtnControl = new TextBoxButtonControl();
                this.txtbtnControl.Visible = false;
                this.dataGridView1.Controls.Add(this.txtbtnControl);
    
                this.txtbtnControl.btnCode.Click += new EventHandler(btnCode_Click);
                this.txtbtnControl.Leave += new EventHandler(txtbtnControl_Leave);
                this.txtbtnControl.txtCode.TextChanged += new EventHandler(txtCode_TextChanged);
    
                this.dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
                this.dataGridView1.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGridView1_ColumnHeaderMouseClick);
                this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
            }
    
            private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                this.txtbtnControl.Visible = false;
            }
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == Code.Index)
                {
                    try
                    {
                        Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
                        this.txtbtnControl.Location = rect.Location;
                        this.txtbtnControl.Size = rect.Size;
                        this.txtbtnControl.btnCode.Text = "..";
                        this.txtbtnControl.Visible = true;
                        this.txtbtnControl.Focus();
                        if (!string.IsNullOrEmpty(this.dataGridView1.CurrentRow.Cells[Code.Name].Value.ToString()))
                        {
                            this.txtbtnControl.txtCode.Text = this.dataGridView1.CurrentRow.Cells[Code.Name].Value.ToString();
                        }
                        else
                        {
                            this.txtbtnControl.txtCode.Text = "";
                        }
                    }
                    catch (Exception) { }
                }
            }
    
            private void txtCode_TextChanged(object sender, EventArgs e)
            {
                if (this.txtbtnControl.txtCode.Modified)
                {
                    this.dataGridView1.CurrentRow.Cells[Code.Name].Value = this.txtbtnControl.txtCode.Text;
                }
            }
    
            private void txtbtnControl_Leave(object sender, EventArgs e)
            {
                try
                {
                    this.txtbtnControl.Visible = false;
                    this.txtbtnControl.txtCode.Text = "";
                }
                catch (Exception)
                {
                }
            }
    
            private void btnCode_Click(object sender, EventArgs e)
            {
                //Form2 form2 = new Form2();
                //form2.ShowDialog();
                OpenFileDialog dialog = new OpenFileDialog();
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value = dialog.FileName.ToString();
                        txtbtnControl.txtCode.Text = dialog.FileName.ToString();
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
    
        class TextBoxButtonControl : UserControl
        {
            public TextBox txtCode;
            public Button btnCode;
    
            public TextBoxButtonControl()
            {
                this.txtCode = new TextBox();
                this.Controls.Add(this.txtCode);
                this.btnCode = new Button();
                this.Controls.Add(this.btnCode);
                this.renderControl();
            }
            public void renderControl()
            {
                this.txtCode.Location = new Point(0, 0);
                //this.txtCode.Width = 2 * this.Width / 3 - 17;
                this.txtCode.Width = this.Width + 115;
                this.txtCode.Height = this.Height;
                this.btnCode.Location = new Point(this.Width + 115, 0);
                this.btnCode.Width = 32;
                this.btnCode.Height = 21;
            }
        }
    
    Hope it helps

    • Marked as answer by Bob Wu-MT Friday, November 25, 2011 2:48 AM
    Thursday, November 10, 2011 10:22 AM
  • Hi,

     

    This is what i am expecting, I need one more clarification. In the above implementation after click on textbox control browse button is displayed. But I need it should display when new row is added and also it should not get disappear.

     

    Thanks in advance.

    Sunday, November 27, 2011 5:57 AM
  • Hi sanjuG,

    Please try to replace this.txtbtnControl.Visible = false with this.txtbtnControl.Visible = true.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, November 28, 2011 9:12 AM
  • Hi,

    I made a changed as you told, but when i add new row to datagridview again it is hiding the txtbnControl. I need txtbnControl should visible always even after new row is added.

     

    Thanks in advance.

    Monday, December 5, 2011 4:26 AM
  • I need txtbnControl should visible always even after new row is added.


    Hi sanjuG,
    Use two Column instead, please tries my code.
    Best Regards,
    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, December 6, 2011 10:43 AM
  • Hi Bob,

    Thanks for your help, but i need both the control in same column as Hardz T L mentioned, it is working as per my expect ion but when i add new row button is getting disappears. Can you please help me out with this.

    One more point after pressing button I can able to get row index also.

     

    Please check with Hardz code and give me a solution.

     

    Regards,

    Sanju.

    Wednesday, December 7, 2011 4:48 AM
  • Hi sanjuG,
    I don’t think we can do this with Hardz’s code, please try the code below.
    void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            DataGridView grid = sender as DataGridView;
            int pad =
            grid.Columns[e.ColumnIndex].DefaultCellStyle.Padding.Left;
            if (pad > 5)
            {
                Rectangle rect = grid.GetCellDisplayRectangle(e.ColumnIndex,
        e.RowIndex, false);
                rect.X += rect.Width - pad;
                rect.Y += 2;
                rect.Width = pad;
                rect.Height = rect.Height - 4;
                if (rect.Contains(grid.PointToClient(Control.MousePosition)))
                {
                    //MessageBox.Show("Click");
                    OpenFileDialog dialog = new OpenFileDialog();
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        try
                        {
                            dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value = dialog.FileName.ToString();
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
        }
        catch
        {
        }
    }
    
    void dataGridView1_CellPainting(object sender,
    DataGridViewCellPaintingEventArgs e)
    {
        if (e.CellStyle.Padding.Left > 5)
        {
            DataGridView grid = sender as DataGridView;
            e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
            Rectangle rect = e.CellBounds;
            rect.X += rect.Width - (e.CellStyle.Padding.Left - 1);
            rect.Y += 2;
            rect.Width = e.CellStyle.Padding.Left - 1;
            rect.Height = rect.Height - 4;
            ButtonState state = (Control.MouseButtons ==
        MouseButtons.Right
                                &&
        rect.Contains(grid.PointToClient(Control.MousePosition)))
                            ? ButtonState.Pushed : ButtonState.Normal;
            ControlPaint.DrawButton(e.Graphics, rect, state);
            e.Handled = true;
        }
    }
    
    private void Form1_Load(object sender, EventArgs e)
    
    {
        this.dataGridView1.Columns.Add("", "");
        this.dataGridView1.Columns[0].DefaultCellStyle.Padding =
    new Padding(20, 0, 0, 0);
        this.dataGridView1.CellPainting += new
    DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
        this.dataGridView1.CellClick += new
    DataGridViewCellEventHandler(dataGridView1_CellClick);
    
        this.dataGridView1.RowCount = 5;
    }
    

    Another solution is use Zhi-Xin Ye’s code here and change it according to your requirement.
    Best Regards,

    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    • Proposed as answer by Hardz Tarrayo Friday, December 9, 2011 12:12 AM
    Wednesday, December 7, 2011 8:23 AM
  • Hi Bob,

    Thanks for your help, could you please tell me how to provide text "..."(three dots) on the button control in your above code.

    Button control should look like browse button.

    Please tell me how to do it in your code.

    Regards,

    Sanju.

    Thursday, December 8, 2011 5:07 AM
  • Hi sanju,
    Replace dataGridView1_CellPainting with the following code, the three new added line will draw the text in the button.

    void dataGridView1_CellPainting(object sender,
    DataGridViewCellPaintingEventArgs e)
    {
        if (e.CellStyle.Padding.Left > 5)
        {
            DataGridView grid = sender as DataGridView;
            e.Paint(e.ClipBounds, DataGridViewPaintParts.All);
            Rectangle rect = e.CellBounds;
            rect.X += rect.Width - (e.CellStyle.Padding.Left - 1);
            rect.Y += 2;
            rect.Width = e.CellStyle.Padding.Left - 1;
            rect.Height = rect.Height - 4;
            ButtonState state = (Control.MouseButtons ==
        MouseButtons.Right
                                &&
        rect.Contains(grid.PointToClient(Control.MousePosition)))
                            ? ButtonState.Pushed : ButtonState.Normal;
            ControlPaint.DrawButton(e.Graphics, rect, state);
    
            StringFormat formater = new StringFormat();//added
            formater.Alignment = StringAlignment.Center;//added
            e.Graphics.DrawString("...", e.CellStyle.Font, new SolidBrush(e.CellStyle.ForeColor), rect, formater);//added
            e.Handled = true;
        }
    }<br/>Best regards,
    


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us
    • Proposed as answer by Hardz Tarrayo Friday, December 9, 2011 12:12 AM
    Thursday, December 8, 2011 6:41 AM
  • button is not visible when we enter data on that row.....please rply!!!!!

    Manoj

    Thursday, June 28, 2012 3:58 PM
  • Hi Manoj,

    Since this thread is quite old, could you please create a new thread for the issue?

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Bob Wu-MT Monday, July 2, 2012 6:44 AM
    Monday, July 2, 2012 6:44 AM
  • Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'DataGridView1.GridColor = Color.Red
            Dim btn As New Button
            btn.FlatStyle = FlatStyle.System
            btn.Text = "..."
            btn.TextAlign = ContentAlignment.MiddleCenter
            btn.Width = 16
            btn.Height = 17
            Dim X As Integer = DataGridView1.Columns(0).Width + DataGridView1.Columns(1).Width + DataGridView1.RowHeadersWidth - btn.Width
            Dim Y As Integer = DataGridView1.Rows(0).Height + 1
            btn.Location = New Point(X, Y)
            DataGridView1.Controls.Add(btn)
            '''''''Adjust Button Location X and Y  on Re-Size of Coloumn Header
        End Sub

    visit: how2doinvbdotnet.blogspot.in/2013/10/how-to-add-browse-button-to.html

    Wednesday, October 23, 2013 6:08 AM