9 个解决方案
#1
通过设置DataGrid的TableStyle,增加DataGridTextBoxColumn,设置其的Alignment为Right即可。
#2
不行的
FormattedTextBoxColumn dc=new FormattedTextBoxColumn(dt.Columns[sKc]);
dc.MappingName = dt.Columns[i].ColumnName;
dc.HeaderText = dt.Columns[i].ColumnName;
dc.NullText = "";
dc.Alignment = HorizontalAlignment.Right;
dc.Width = 40;
ts.GridColumnStyles.Add(dc);
这个只能标题向右,因为我Paint了DataGridTextBoxColumn
FormattedTextBoxColumn dc=new FormattedTextBoxColumn(dt.Columns[sKc]);
dc.MappingName = dt.Columns[i].ColumnName;
dc.HeaderText = dt.Columns[i].ColumnName;
dc.NullText = "";
dc.Alignment = HorizontalAlignment.Right;
dc.Width = 40;
ts.GridColumnStyles.Add(dc);
这个只能标题向右,因为我Paint了DataGridTextBoxColumn
#3
你怎么重画的
#4
每列中都加<ItemStyle HorizontalAlign="Right" />
#5
web程序?
datagrid的autogeneratecolumns属性是什么,应该为false才对。
datagrid的autogeneratecolumns属性是什么,应该为false才对。
#6
我是WinForm的
protected override void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum, Brush BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
Object data = ( Object ) GetColumnValueAtRow(Source, RowNum);
string strData,strItemNo ;
int nFlag = 0;
strData = data.ToString();
strItemNo = dc.Table.Rows[RowNum][0].ToString();
if(strData !="" && strData != null)
{
FormatEventArgs e = new FormatEventArgs(RowNum) ;
g.FillRectangle(e.BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);
FontStyle fs = FontStyle.Regular ;
if( e.strikeThrough == true )
{
fs = FontStyle.Strikeout ;
}
System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif, (float)8.5 ,fs);
string sSql= string.Format("SELECT ItemNo,MaxNum,MinNum FROM PUBItemMaster WHERE ItemNo='{0}'",strItemNo);
_dtTable = CT.SelectDataBase(sSql);
if(_dtTable.Rows.Count !=0)
{
if(_dtTable.Rows[0][1].ToString() != System.DBNull.Value.ToString())
{
if(float.Parse(strData) > float.Parse(_dtTable.Rows[0][1].ToString()))
{
nFlag = 1;
}
}
if(_dtTable.Rows[0][2].ToString() != System.DBNull.Value.ToString())
{
if(float.Parse(strData) < float.Parse(_dtTable.Rows[0][2].ToString()))
{
nFlag = 2;
}
}
}
if(nFlag == 1)
{
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds.X ,Bounds.Y);
}
else if(nFlag == 2)
{
g.DrawString( strData ,font ,Brushes.Red ,Bounds.X ,Bounds.Y);
}
else
{
g.DrawString( strData ,font ,Brushes.Black ,Bounds.X ,Bounds.Y);
}
}
}
protected override void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum, Brush BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
Object data = ( Object ) GetColumnValueAtRow(Source, RowNum);
string strData,strItemNo ;
int nFlag = 0;
strData = data.ToString();
strItemNo = dc.Table.Rows[RowNum][0].ToString();
if(strData !="" && strData != null)
{
FormatEventArgs e = new FormatEventArgs(RowNum) ;
g.FillRectangle(e.BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);
FontStyle fs = FontStyle.Regular ;
if( e.strikeThrough == true )
{
fs = FontStyle.Strikeout ;
}
System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif, (float)8.5 ,fs);
string sSql= string.Format("SELECT ItemNo,MaxNum,MinNum FROM PUBItemMaster WHERE ItemNo='{0}'",strItemNo);
_dtTable = CT.SelectDataBase(sSql);
if(_dtTable.Rows.Count !=0)
{
if(_dtTable.Rows[0][1].ToString() != System.DBNull.Value.ToString())
{
if(float.Parse(strData) > float.Parse(_dtTable.Rows[0][1].ToString()))
{
nFlag = 1;
}
}
if(_dtTable.Rows[0][2].ToString() != System.DBNull.Value.ToString())
{
if(float.Parse(strData) < float.Parse(_dtTable.Rows[0][2].ToString()))
{
nFlag = 2;
}
}
}
if(nFlag == 1)
{
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds.X ,Bounds.Y);
}
else if(nFlag == 2)
{
g.DrawString( strData ,font ,Brushes.Red ,Bounds.X ,Bounds.Y);
}
else
{
g.DrawString( strData ,font ,Brushes.Black ,Bounds.X ,Bounds.Y);
}
}
}
#7
上面那个是我重划的代码,功能是循环取出dataGrid中列为"数量"的值,然后在Paint中该行"数量"对应的编号从数据库中取出该编号对应的最大值和最小值,然后把"数量"和最大值比较大小,再把"数量"和最小值比较大小,如果小于最小值则文字显示为Red,如果大于最大值则文字显示为DarkOrange,否则正常显示为Black.
问题是我Paint后,我在DataGridTableStyle样式里用HorizontalAlignment.Right向右还是在左边,只有列名向右了,如果不paint就可以使列名和内容向右了,请大家帮忙,谢谢了
问题是我Paint后,我在DataGridTableStyle样式里用HorizontalAlignment.Right向右还是在左边,只有列名向右了,如果不paint就可以使列名和内容向右了,请大家帮忙,谢谢了
#8
change
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds.X ,Bounds.Y);
with
StringFormat myFormat = new StringFormat();
myFormat.Alignment = StringAlignment.Right;
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds, myFormat);
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds.X ,Bounds.Y);
with
StringFormat myFormat = new StringFormat();
myFormat.Alignment = StringAlignment.Right;
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds, myFormat);
#9
谢谢Knight94(愚翁) ,解决了,结贴
#1
通过设置DataGrid的TableStyle,增加DataGridTextBoxColumn,设置其的Alignment为Right即可。
#2
不行的
FormattedTextBoxColumn dc=new FormattedTextBoxColumn(dt.Columns[sKc]);
dc.MappingName = dt.Columns[i].ColumnName;
dc.HeaderText = dt.Columns[i].ColumnName;
dc.NullText = "";
dc.Alignment = HorizontalAlignment.Right;
dc.Width = 40;
ts.GridColumnStyles.Add(dc);
这个只能标题向右,因为我Paint了DataGridTextBoxColumn
FormattedTextBoxColumn dc=new FormattedTextBoxColumn(dt.Columns[sKc]);
dc.MappingName = dt.Columns[i].ColumnName;
dc.HeaderText = dt.Columns[i].ColumnName;
dc.NullText = "";
dc.Alignment = HorizontalAlignment.Right;
dc.Width = 40;
ts.GridColumnStyles.Add(dc);
这个只能标题向右,因为我Paint了DataGridTextBoxColumn
#3
你怎么重画的
#4
每列中都加<ItemStyle HorizontalAlign="Right" />
#5
web程序?
datagrid的autogeneratecolumns属性是什么,应该为false才对。
datagrid的autogeneratecolumns属性是什么,应该为false才对。
#6
我是WinForm的
protected override void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum, Brush BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
Object data = ( Object ) GetColumnValueAtRow(Source, RowNum);
string strData,strItemNo ;
int nFlag = 0;
strData = data.ToString();
strItemNo = dc.Table.Rows[RowNum][0].ToString();
if(strData !="" && strData != null)
{
FormatEventArgs e = new FormatEventArgs(RowNum) ;
g.FillRectangle(e.BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);
FontStyle fs = FontStyle.Regular ;
if( e.strikeThrough == true )
{
fs = FontStyle.Strikeout ;
}
System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif, (float)8.5 ,fs);
string sSql= string.Format("SELECT ItemNo,MaxNum,MinNum FROM PUBItemMaster WHERE ItemNo='{0}'",strItemNo);
_dtTable = CT.SelectDataBase(sSql);
if(_dtTable.Rows.Count !=0)
{
if(_dtTable.Rows[0][1].ToString() != System.DBNull.Value.ToString())
{
if(float.Parse(strData) > float.Parse(_dtTable.Rows[0][1].ToString()))
{
nFlag = 1;
}
}
if(_dtTable.Rows[0][2].ToString() != System.DBNull.Value.ToString())
{
if(float.Parse(strData) < float.Parse(_dtTable.Rows[0][2].ToString()))
{
nFlag = 2;
}
}
}
if(nFlag == 1)
{
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds.X ,Bounds.Y);
}
else if(nFlag == 2)
{
g.DrawString( strData ,font ,Brushes.Red ,Bounds.X ,Bounds.Y);
}
else
{
g.DrawString( strData ,font ,Brushes.Black ,Bounds.X ,Bounds.Y);
}
}
}
protected override void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum, Brush BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
Object data = ( Object ) GetColumnValueAtRow(Source, RowNum);
string strData,strItemNo ;
int nFlag = 0;
strData = data.ToString();
strItemNo = dc.Table.Rows[RowNum][0].ToString();
if(strData !="" && strData != null)
{
FormatEventArgs e = new FormatEventArgs(RowNum) ;
g.FillRectangle(e.BackBrush, Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height);
FontStyle fs = FontStyle.Regular ;
if( e.strikeThrough == true )
{
fs = FontStyle.Strikeout ;
}
System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif, (float)8.5 ,fs);
string sSql= string.Format("SELECT ItemNo,MaxNum,MinNum FROM PUBItemMaster WHERE ItemNo='{0}'",strItemNo);
_dtTable = CT.SelectDataBase(sSql);
if(_dtTable.Rows.Count !=0)
{
if(_dtTable.Rows[0][1].ToString() != System.DBNull.Value.ToString())
{
if(float.Parse(strData) > float.Parse(_dtTable.Rows[0][1].ToString()))
{
nFlag = 1;
}
}
if(_dtTable.Rows[0][2].ToString() != System.DBNull.Value.ToString())
{
if(float.Parse(strData) < float.Parse(_dtTable.Rows[0][2].ToString()))
{
nFlag = 2;
}
}
}
if(nFlag == 1)
{
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds.X ,Bounds.Y);
}
else if(nFlag == 2)
{
g.DrawString( strData ,font ,Brushes.Red ,Bounds.X ,Bounds.Y);
}
else
{
g.DrawString( strData ,font ,Brushes.Black ,Bounds.X ,Bounds.Y);
}
}
}
#7
上面那个是我重划的代码,功能是循环取出dataGrid中列为"数量"的值,然后在Paint中该行"数量"对应的编号从数据库中取出该编号对应的最大值和最小值,然后把"数量"和最大值比较大小,再把"数量"和最小值比较大小,如果小于最小值则文字显示为Red,如果大于最大值则文字显示为DarkOrange,否则正常显示为Black.
问题是我Paint后,我在DataGridTableStyle样式里用HorizontalAlignment.Right向右还是在左边,只有列名向右了,如果不paint就可以使列名和内容向右了,请大家帮忙,谢谢了
问题是我Paint后,我在DataGridTableStyle样式里用HorizontalAlignment.Right向右还是在左边,只有列名向右了,如果不paint就可以使列名和内容向右了,请大家帮忙,谢谢了
#8
change
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds.X ,Bounds.Y);
with
StringFormat myFormat = new StringFormat();
myFormat.Alignment = StringAlignment.Right;
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds, myFormat);
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds.X ,Bounds.Y);
with
StringFormat myFormat = new StringFormat();
myFormat.Alignment = StringAlignment.Right;
g.DrawString( strData ,font ,Brushes.DarkOrange,Bounds, myFormat);
#9
谢谢Knight94(愚翁) ,解决了,结贴