Click here to Skip to main content
16,021,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i try to to something like this

           ID Column          Text Column
       |-----------------------------------------------|
       |                |      TextBox1                |
ROW 0  |     ID         |------------------------------|
       |                |      TextBox2                |
       |-----------------------------------------------|


As far as i can tell i need to create my own Cell that does have 2 Texboxes in it and Paint that but i´m really not sure how do do that and i only found examples to add one component but not for more in a cell :). Can someone maybe give me an example ?

Thanks in advance ;)


DataGridView2TextBoxCell Class
C#
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataGRidViewCellTest
{
    public class DataGridView2TextBoxCell : DataGridViewTextBoxCell
    {
        public override System.Type FormattedValueType
        {
            get
            {
                return Type.GetType("System.String");
            }
        }
    
        protected override bool SetValue(int rowIndex, object value)
        {
            return base.SetValue(rowIndex, value);
        }
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            // Draw the cell background, if specified.
            if ((paintParts & DataGridViewPaintParts.Background) ==
                DataGridViewPaintParts.Background)
            {
                SolidBrush cellBackground =
                    new SolidBrush(cellStyle.BackColor);
                graphics.FillRectangle(cellBackground, cellBounds);
                cellBackground.Dispose();
            }
            // Draw the cell borders, if specified.
            if ((paintParts & DataGridViewPaintParts.Border) ==
                DataGridViewPaintParts.Border)
            {
                PaintBorder(graphics, clipBounds, cellBounds, cellStyle,
                    advancedBorderStyle);
            }

            // Calculate the draw area for TextBox1
            Rectangle TextBoxArea = cellBounds;
            Rectangle TextBoxAdjustment = this.BorderWidths(advancedBorderStyle);
            TextBoxArea.X += TextBoxAdjustment.X;
            TextBoxArea.Y += TextBoxAdjustment.Y;
            TextBoxArea.Height = TextBoxArea.Height/2;
            TextBoxArea.Width -= TextBoxAdjustment.Width;

            // Calculate the draw area for TextBox2
            Rectangle TextBoxArea2 = cellBounds;
            Rectangle TextBoxAdjustment2 = this.BorderWidths(advancedBorderStyle);
            TextBoxArea2.X += TextBoxAdjustment2.X;
            TextBoxArea2.Y += TextBoxAdjustment2.Y+TextBoxArea.Height;
            TextBoxArea2.Height -= TextBoxArea.Height;
            TextBoxArea2.Width -= TextBoxAdjustment2.Width;

            //to make it writable
            this.ReadOnly = false;

            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            if (TextBoxRenderer.IsSupported)
            {
                //Get the parent column
                DataGridView2TextBoxColumn parent = (DataGridView2TextBoxColumn)this.OwningColumn;
                string Value1 = "Encrypted"; //Encrypted Text
                string Value2 = Convert.ToString(value); //Decrypted Text

                TextBoxRenderer.DrawTextBox(graphics, TextBoxArea, Value1, this.DataGridView.Font, System.Windows.Forms.VisualStyles.TextBoxState.Disabled);
                TextBoxRenderer.DrawTextBox(graphics, TextBoxArea2, Value2, this.DataGridView.Font, System.Windows.Forms.VisualStyles.TextBoxState.Hot);
            }

        }

    }
}



DataGridView2TextBoxColumn Class
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataGRidViewCellTest
{
    public class DataGridView2TextBoxColumn : DataGridViewColumn
    {
        public DataGridView2TextBoxColumn()
        {
            this.CellTemplate = new DataGridView2TextBoxCell();
        }

        public void doSomething()
        {
        }

    }
}
Posted
Updated 15-Jun-15 0:46am
v5
Comments
Kornfeld Eliyahu Peter 14-Jun-15 8:41am    
Learn about cell template...
Bernd Keilmann 15-Jun-15 6:43am    
Well it´s not like i didn´t read anything however i got it almost working but i did run into some problems.
- if i use a normal TextBox Column for the first Column (my Counter) and use my own Class the Counter Text get "overpaint" if i scroll.
-if i click on the Cell to edit it, the second TextBox fills the whole cell so that you can´t see the other TextBox above it.

Edit: added the Code for the Class in my Question ;)

1 solution

do something like this

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
ShowHeaderWhenEmpty="True" Width="100%">
<Columns>
<asp:BoundField DataField="EmployeeId" HeaderText="Employee Id" />
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:TemplateField>
<ItemTemplate>
<table width="100%" cellpadding="2" cellspacing="2">
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ForeColor="red" runat="server" Text="*" ControlToValidate="TextBox1" SetFocusOnError="true" ErrorMessage="Please Enter Amount ">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ForeColor="red" runat="server" Text="*" ControlToValidate="TextBox2" SetFocusOnError="true" ErrorMessage="Please Enter Amount ">
</asp:RequiredFieldValidator>
</td>
</tr>

</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
 
Share this answer
 
Comments
Bernd Keilmann 15-Jun-15 6:55am    
Thank you for the answer but if i´m not wrong this is for Web Applications but i would need that in C#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900