動(dòng)態(tài)改變gridview列寬度函數(shù)分享
我通常用GridView綁定datatable,由于需要?jiǎng)討B(tài)綁定到不同的datatable所以需要?jiǎng)討B(tài)調(diào)整GridView的寬度。所以寫了這個(gè)函數(shù)實(shí)現(xiàn)該功能。GridView的寬度需要根據(jù)各個(gè)列中最大寬度來累加獲得。在求各個(gè)列的最大字符寬度的時(shí)候需要對(duì)中文和英文加以區(qū)分,因?yàn)樽址?ldquo;序號(hào)”和“id”的length屬性都為2,但是顯示的時(shí)候一個(gè)漢字占據(jù)的寬度卻相當(dāng)于2個(gè)英文字符。要想達(dá)到準(zhǔn)確的顯示效果,我對(duì)含有漢字的字符串根據(jù)漢字的數(shù)目確定該字符串等價(jià)英文字符的長(zhǎng)度,例如字符串“序號(hào)id”的length屬性為4,我自己通過函數(shù)獲得的長(zhǎng)度為6.確定了每列的最大字符數(shù)后,累加即可獲得GridView的寬度字符,然后乘于一個(gè)字符在屏幕上的顯示寬度oneLetterLength常量后就是GridView寬度。
public void SetGridViewWidth(GridView gridview1)
{
int rowcount = gridview1.Rows.Count; //行數(shù)
int colcount = gridview1.Columns.Count; //列數(shù)
int i=0,j=0;
int[] cellwidth = new int[colcount]; //數(shù)組用來存儲(chǔ)各個(gè)列的最大字符數(shù)
int gridviewwidth = 0; //GridView寬度
Unit width = 0;
string temp = null;
int tempLength = 0;
for (i = 0; i < rowcount; i++) //循環(huán)數(shù)據(jù)項(xiàng),獲得各個(gè)列的最大字符寬度
{
for (j = 0; j < colcount; j++)
{
temp = gridview1.Rows[i].Cells[j].Text;
tempLength = LengthOfLetter(temp); //LengthOfLetter()返回含中文的字符串字符寬度,1個(gè)漢字2個(gè)字符寬
if (cellwidth[j] < tempLength)
{
cellwidth[j] = tempLength; //存儲(chǔ)較大寬度值
}
}
}
for (j = 0; j < colcount; j++)
{
if (gridview1.HeaderRow.Visible == true) //如果GridView表頭可見,將表頭列寬參與比較 {
temp = gridview1.HeaderRow.Cells[j].Text;
tempLength = LengthOfLetter(temp);
if (cellwidth[j] < tempLength)
{
cellwidth[j] = tempLength;
}
}
if (gridview1.FooterRow.Visible == true) //如果GridView表尾可見,將表尾列寬參與比較 {
temp = gridview1.FooterRow.Cells[j].Text;
tempLength = LengthOfLetter(temp);
if (cellwidth[j] < tempLength)
{
cellwidth[j] = tempLength;
}
}
}
for (j = 0; j < colcount; j++)
{
if (gridview1.Columns[j].Visible == true) //將顯示的列的各列最大字符寬度相加 {
gridviewwidth += cellwidth[j];
}
}
width = gridviewwidth * oneLetterLength; //GridView最大字符數(shù)乘于一個(gè)字符顯示寬度得到GridView顯示寬度
if (gridview1.Width.Value < width.Value) //如果在界面上已經(jīng)設(shè)置了GridView的寬度,將動(dòng)態(tài)求的寬度和頁面上的
{ //初始化寬度比較,如果初始化寬度較小則將寬度設(shè)置為新調(diào)整的寬度。
gridview1.Width = width;
}
}
//含有中文的字符串等效英文字符串顯示長(zhǎng)度
public int LengthOfLetter(string temp)
{
int length = temp.Length;
int newlength = temp.Length;
for (int i = 0; i < length; i++) //遍歷字符串每個(gè)字符
{
if (IsChineseLetter(temp, i)) //IsChineseLetter()判斷是否為中文字符,是則寬度加1
{
newlength++;
}
}
return newlength;
}
//判斷是否為中文字符
public bool IsChineseLetter(string input,int index)
{
int code = 0;
int chfrom = Convert.ToInt32("4e00", 16); //范圍(0x4e00~0x9fff)轉(zhuǎn)換成int(chfrom~chend)
int chend = Convert.ToInt32("9fff", 16);
if (input != "")
{
code = Char.ConvertToUtf32(input, index); //獲得字符串input中指定索引index處字符unicode編碼
if (code >= chfrom && code <= chend)
{
return true; //當(dāng)code在中文范圍內(nèi)返回true
}
else
{
return false ; //當(dāng)code不在中文范圍內(nèi)返回false
}
}
return false;
}
相關(guān)文章
C#中32位浮點(diǎn)數(shù)Float(Real)一步步按位Bit進(jìn)行分析
這篇文章主要介紹了C#中32位浮點(diǎn)數(shù)Float(Real)一步步按位Bit進(jìn)行分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08c#使用DotNetZip封裝類操作zip文件(創(chuàng)建/讀取/更新)實(shí)例
DotnetZip是一個(gè)開源類庫,支持.NET的任何語言,可很方便的創(chuàng)建,讀取,和更新zip文件。而且還可以使用在.NETCompact Framework中。2013-11-11C#使用BinaryFormatter類、ISerializable接口、XmlSerializer類進(jìn)行序列化和反序列
這篇文章介紹了C#使用BinaryFormatter類、ISerializable接口、XmlSerializer類進(jìn)行序列化和反序列化的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09