تغییر اندازه سایز header در دیتاگرید

nafiseh_hch

Member
دوباره سلام :)
توی فرم من یه دیتاگرید هست که بصورت زیر پرش کردم
کد:
[SIZE=2]
cmd = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlCommand([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"SELECT Id, FdoOffice, CoName, comptxt2 FROM Comission01 where comptxt2 is not null"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], MySqlConnection)
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] MySqlConnection.State = ConnectionState.Closed [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] MySqlConnection.Open()
myDA = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SqlDataAdapter(cmd)
myDataSet = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DataSet()
myDA.Fill(myDataSet, [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" Comission01"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])
dv = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] DataView(myDataSet.Tables([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" Comission01"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]))
dg1.DataSource = dv
dv.Sort = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"FdoOffice DESC"
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]dg1.Columns([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"FdoOffice"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).HeaderText = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]""
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]dg1.Columns([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"comptxt2"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).HeaderText = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" "
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]dg1.Columns([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"CoName"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).HeaderText = [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]" "
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]dg1.Columns([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Id"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).Visible = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]dg1.Columns([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"comptxt2"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).ReadOnly = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]dg1.Columns([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"CoName"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).ReadOnly = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]dg1.Columns([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"FdoOffice"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]).ReadOnly = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]dg1.Columns.Item(0).Width = 40
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000][/COLOR][/SIZE][/COLOR][/SIZE]

وقتی اجرا میکنم درست اجرا میشه ولی وقتی فرم رو می بندم دوباره باز می کنم میاد روی خط آخر این پیغام رو میده
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
من چجوری میتونم برای ستون های دیتاگریدم اندازه تعیین کنم؟؟
وی اینکه چجوری header ها رو غیر قابل کلیک کنم برای sort
چون اونم error میده
 

the_king

مدیرکل انجمن
دوباره سلام :)
توی فرم من یه دیتاگرید هست که بصورت زیر پرش کردم
کد:
cmd = New SqlCommand("SELECT Id, FdoOffice, CoName, comptxt2 FROM Comission01 where comptxt2 is not null", MySqlConnection)
If MySqlConnection.State = ConnectionState.Closed Then MySqlConnection.Open()
myDA = New SqlDataAdapter(cmd)
myDataSet = New DataSet()
myDA.Fill(myDataSet, " Comission01")
dv = New DataView(myDataSet.Tables(" Comission01"))
dg1.DataSource = dv
dv.Sort = "FdoOffice DESC"
dg1.Columns("FdoOffice").HeaderText = ""
dg1.Columns("comptxt2").HeaderText = " "
dg1.Columns("CoName").HeaderText = " "
dg1.Columns("Id").Visible = False
dg1.Columns("comptxt2").ReadOnly = True
dg1.Columns("CoName").ReadOnly = True
dg1.Columns("FdoOffice").ReadOnly = True
dg1.Columns.Item(0).Width = 40

وقتی اجرا میکنم درست اجرا میشه ولی وقتی فرم رو می بندم دوباره باز می کنم میاد روی خط آخر این پیغام رو میده
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
من چجوری میتونم برای ستون های دیتاگریدم اندازه تعیین کنم؟؟

لطفا زبان برنامه نویسی رو حتما در عنوان سوال بنویسید که سایر کاربران بدانند که تاپیک در مورد چه زبانی یه.

وقتی فرم رو می بندید ()Dispose اش می کنید؟ اگر اینکار رو انجام ندهید ممکنه در حالت قبلی در حافظه
باقی بمونه و وضعیت اش دقیقا مثل سابق نباشه. دفعه دوم کدتان در وضعیتی اجرا میشه که تعداد ستون های
جدول 0 است و (dg1.Columns.Item(0 وجود نداره.
بهتره که ستون ها رو با اسم شون مورد اشاره قرار بدهید، مثلا dg1.Columns("FdoOffice").Width = 40

وی اینکه چجوری header ها رو غیر قابل کلیک کنم برای sort
چون اونم error میده
با کدی مثل این :
کد:
        DataGridView3.Columns("CoName").SortMode = DataGridViewColumnSortMode.NotSortable
 

جدیدترین ارسال ها

بالا