如何在DataGridViewCell中添加多个TextBox [英] How to add multiple TextBoxes in a DataGridViewCell
本文介绍了如何在DataGridViewCell中添加多个TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我尝试这样的事情
Hello i try to to something like this
ID Column Text Column
|-----------------------------------------------|
| | TextBox1 |
ROW 0 | ID |------------------------------|
| | TextBox2 |
|-----------------------------------------------|
据我所知,我需要创建自己的Cell,其中包含2个Texbox并绘制但是我真的不知道怎么做的,我只找到了添加一个组件的例子但在单元格中没有更多:)有人可以举个例子吗?
提前致谢;)
DataGridView2TextBoxCell类
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
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类
DataGridView2TextBoxColumn Class
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()
{
}
}
}
推荐答案
做类似的事情
< asp:GridView ID =GridView1runat =serverAutoGenerateColumns =false
ShowHeaderWhenEmpty =TrueWidth = 100%>
< Columns>
< asp:BoundField DataField =EmployeeIdHeaderText =Employee Id/>
< asp:BoundField DataField =FirstNameHeaderText =First Name/>
< asp:BoundField DataField =LastNameHeaderText =姓氏/>
< asp:TemplateField>
< ItemTemplate>
< table width =100%cellpadding =2cellspacing =2 >
< tr>
< td>
< asp:TextBox ID =TextBox1runat =server>< / asp:TextBox>
< asp:RequiredFieldValidator ID =RequiredFieldValidator1ForeColor =redrunat =serverText =*ControlToValidate =TextBox1SetFocusOnError =trueErrorMessage =Please Enter Amount >
< / asp:RequiredFieldValidator>
< / td>
< / tr>
< tr>
< td>
< asp:TextBox ID =TextBox2runat =server>< / asp:TextBox>
< asp:RequiredFieldValidator ID =RequiredFieldValidator2ForeColor =redrunat =serverText =*ControlToValidate =TextBox2SetFocusOnError =trueErrorMessage =Please Enter Amount >
< / asp:RequiredFieldValidator>
< / td>
< / tr>
< / table>
< / ItemTemplate>
< / asp:TemplateField>
< / Columns>
< / asp:GridView>
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>
这篇关于如何在DataGridViewCell中添加多个TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文