473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DataGridTextBoxColumn

21
hi,

i am trying to make some columns of my datagrid editable by making them become textboxes once clicked on any cell in the column. i m trying to use DataGridTextBoxColumn... i m trying to code it in c# in VS.NET....but i have no idea how to go abt it...can anyone pls provide me the code snippet for this if u hv....plsss helllppppp!!!! thanks
Mar 1 '07 #1
15 5116
radcaesar
759 Expert 512MB
Public Class DataGridCurrencyColumn

Inherits System.Windows.Forms.DataGridTextBoxColumn

Dim TB As System.Windows.forms.TextBox

Public Sub New()

MyBase.New()

TB = Me.TextBox

TB.CausesValidation = True

AddHandler TB.KeyPress, AddressOf CurrencyKeyPress

AddHandler TB.Validating, AddressOf CurrencyValidating

End Sub


Protected Sub CurrencyValidating(ByVal Sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

Dim Val As Decimal

Try

Val = Decimal.Parse(TB.Text)

Catch ex As Exception

Val = 0

End Try

TB.Text = Microsoft.VisualBasic.Format(Val, "0.00")

End Sub

Protected Sub CurrencyKeyPress(ByVal Sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

Dim keyascii As Integer

keyascii = Asc(e.KeyChar)

Select Case keyascii

Case 45, 46, 48 To 57, 8, 13 'numbers, decimal, backspace, return, minus

Case Else

keyascii = 0

End Select

If keyascii = 0 Then

e.Handled = True

Else

e.Handled = False

End If

End Sub


End Class
Mar 1 '07 #2
MCPD
29
i make this code

this view your table in a data grid

u can change any cell u want

but u must to click at the button to save the changes

Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.         SqlConnection con = null;
  8.         SqlDataAdapter da = null;
  9.         DataSet ds = null;
  10.         SqlCommandBuilder cb = null;
  11.         private void Form1_Load(object sender, EventArgs e)
  12.         {
  13.             con = new SqlConnection("Data Source=.;Initial Catalog=CDDB;Integrated Security=True");
  14.             da = new SqlDataAdapter("select * from CDS",con);
  15.             cb = new SqlCommandBuilder(da);
  16.             ds = new DataSet();
  17.             da.Fill(ds);
  18.             dataGridView1.DataSource = ds.Tables[0];
  19.         }
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.             da.Update(ds);
  24.         }
  25.     }
Mar 1 '07 #3
harini
21
hi,

thanks all for ur valuable replies!

@radcaesar- i m trying to code it in c# in visual studio.net....i want the code snippet in c#....pls do provide me the c# code if u hv....tnx a lot!

@mcpd- hey tnx for the code...but the prob here is i wrote a similar code like u hv given...in this case what it does is, it transports the table into the grid box and ALL the cells of ALL columns are editable...but i dont want that...i want only some columns of my table to be editable...the rest of the columns i shud NOT be able to edit...and the cells of the columns that i want to edit must be displayed in text boxes so that i can edit those values...pls try to give me the code snippet for this functionality...

pls help!
Mar 2 '07 #4
harini
21
@mcpd- when i wrote the code u gave me for data update it says the name ds is not available in the class or namespace. ds is the dataset object i hv given. i hv made sure it is within the scope of the class and namespace and still the same error persists. kindly help!
Mar 2 '07 #5
wertqt
21
hi harini ,
can u share with me the source code for this function? My program requires this part as well. By the way, do u know how to to copy information from some columns in datagridview1 and show it in datagridview2?
For e.g, :
in database1, i have a field(column) called 'Date'. I want to pull this column information into database2 under a column name 'StopDate'

Sorry to trouble. Im quite new to C#.

Thanks!
Mar 5 '07 #6
harini
21
@wertqt- i m still not done with the code...still figuring out stuff! will help u out once i get something concrete...sorry abt that!

@mcpd- ur code is perfect...so how did u set the datagrid TableStyle property? from the properties box or did u hard code the datagrid tablestyles? and only if we set the styles for the datagrid and the columns will this code of urs run right?
Mar 6 '07 #7
radcaesar
759 Expert 512MB
Follow this link, Here you can get all stuffs regarding Edit and update of grid

http://www.codeproject.com/csharp/practicalguidedatagrids4.asp?df=100&forumid=33944& exp=0&select=1284249

:)
Mar 6 '07 #8
Maybee this helps?
Expand|Select|Wrap|Line Numbers
  1. private void MYGrid_EditCommand(object s, DataGridCommandEventArgs e)
  2. {
  3. MYGrid.EditItemIndex = e.Item.ItemIndex;
  4. }
  5.  
Mar 6 '07 #9
harini
21
@wertqt- sorry for the late reply but i just now wrapped up with things. for ur first ques the source code is the same as what MCPD has provided, just mk sure u make all the connections again in the update button if u havent made ur connection global.

as for ur 2nd ques of copying certain columns 4m one datagrid to another i havent tried it...ill try it n if i get the output will share it with u!
Mar 9 '07 #10
harini
21
@wertqt- and if u want to mk some columns in ur datagrid editable and not some other, then go and change the TableStyle property in the property window for ur datagrid...in that set ur table style and also ur columns styles accordingly...if u want to mk a column uneditable then set the read only property of that column as true and vice versa! hope this helps!
Mar 9 '07 #11
wertqt
21
hi harini,

i wrote the code as below to save the changes made to the datagridview textboxes but nothing happen. What i had written into these cells are not saved to the database. Can u detect any errors in it?

C# code :

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             string strSerialNumber = textBox1.Text;
  5.             string strSQL2 = "SELECT * FROM Module_Repair_Info WHERE SerialNo = '" + strSerialNumber + "'";
  6.             string Provider2 = "Provider=Microsoft.Jet.oledb.4.0;Data Source=C:\\Documents and Settings\\Evelyn.Chin\\Desktop\\Repair_database\\Hart\\Temperature_Repair_Database.MDB";
  7.             OleDbConnection myConn2 = new OleDbConnection(Provider2);
  8.             OleDbCommand myCmd2 = new OleDbCommand(strSQL2, myConn2);
  9.             OleDbDataAdapter da2 = new OleDbDataAdapter(strSQL2, myConn2); 
  10.             OleDbCommandBuilder cb = new OleDbCommandBuilder(da2);
  11.             DataSet ds2 = new DataSet();
  12.             da2.Fill(ds2, "Module_Repair_Info");
  13.             BindingSource BS2 = new BindingSource();
  14.             BS2.DataSource = ds2;
  15.             BS2.DataMember = "Module_Repair_Info";
  16.             dataGridView2.DataSource = BS2;
  17.  
  18.             da2.Update(ds2, "Module_Repair_Info");
  19.             ds2.AcceptChanges();
  20.  
  21.  
  22.  
  23.         }
Thanks a million!
Mar 12 '07 #12
harini
21
well i cant say what error ur code has...looks fine! but i can tell u two things that is in ur code which i didnt use in my code...u can mk those changes n try if it works...

1.) i didnt use a BindingSource in my code.
2.) i didnt use an AcceptChange() also...

the foll is the code snippet i iused and it works fine for me

DataSet ds = new DataSet(); .............1
DataTable da = new DataTable();.........2
dadapter.Fill(ds, "table_name");...........3
da = (DataTable)datagrid.DataSource;..4
dadapter.Update(da);..........................5

i used a DataTable because i was getting a "cast not valid" exception when i used a dataset in line 4....line 5 is the only line to update changes back to the dataset and also the database...

hope it helps!
Mar 12 '07 #13
wertqt
21
hi,
I tried ur cpde but it gave me this error : "Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'."

my new code looks like this :

Expand|Select|Wrap|Line Numbers
  1. string strSerialNumber = textBox1.Text;
  2.             string strSQL2 = "SELECT * FROM Module_Repair_Info WHERE SerialNo = '" + strSerialNumber + "'";
  3.             string Provider2 = "Provider=Microsoft.Jet.oledb.4.0;Data Source=C:\\Documents and Settings\\Evelyn.Chin\\Desktop\\Repair_database\\Hart\\Temperature_Repair_Database.MDB";
  4.             OleDbConnection myConn2 = new OleDbConnection(Provider2);
  5.             OleDbCommand myCmd2 = new OleDbCommand(strSQL2, myConn2);
  6.             OleDbDataAdapter da2 = new OleDbDataAdapter(strSQL2, myConn2); 
  7.             OleDbCommandBuilder cb = new OleDbCommandBuilder(da2);
  8.  
  9.             DataSet ds3 = new DataSet(); 
  10.             DataTable dt = new DataTable();
  11.             da2.Fill(ds3, "Module_Repair_Info");
  12.             dt = (DataTable)dataGridView2.DataSource;
  13.             da2.Update(dt);

Thanks =P
Mar 12 '07 #14
harini
21
this code works fine for me...donno y its throwing errors for u...

DataSet ds = new DataSet(); .............1
DataTable da = new DataTable();.........2
dadapter.Fill(ds, "table_name");...........3
da = (DataTable)datagrid.DataSource;..4
dadapter.Update(da);..........................5

ok then try like this...comment out the 2nd line ...i.e dont use a DataTable...and change the 4th line to either

1.) ds = (DataSet)datagrid.DataSource; or
2.) datagrid.DataSource = ds;

hope this works!
Mar 12 '07 #15
wertqt
21
ok i will try it and keep u updated thanks =D
Mar 13 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: DraguVaso | last post by:
Hi, I need to know the Lenght of the Text in a DataGridTextBoxColumn when I move with the mouse over it, and show it in a ToolTip. I have something that works, but it works only when the...
2
by: Job Lot | last post by:
I am customizing DataGridTextBoxColumn. How can I access properties in DataGrid to which custom DataGridTextBoxColumn will be added. Say for instance, how I would access SelectionBackColor...
0
by: DraguVaso | last post by:
Hi, I 'wrote' my own DataGrid, using the normal DataGrid, and overriding some of the DataGridTextBoxColumn-methods (mostly the Paint-method). I based most of my changes on the Windows Form FAQ:...
0
by: Mike Burkhart | last post by:
I have written the following class to build a data grid textbox column that has the KeyUp and KeyPress events defined for the TextBox. I also defined the KeyDown event for testing purposes only. ...
1
by: Jaco Karsten | last post by:
I am writing a customized DataGrid control which will allow the user to only select an entire row (and not a cell inside the row). So I want to add readonly columns to the datagrid. To achieve...
0
by: Jax | last post by:
I am using a class that inherits from the DataGridTextBoxColumn. It adds a combo box into the column where it displays a selection of choices. The problem I have is that when this comboBox loses...
1
by: Sangeetha | last post by:
I have a datagrid column which I want to expand when i type in some text. Initially I tried increasing the column width on the key down event of that DataGridTextBoxColumn, but it didn't work out as...
0
by: JMarques | last post by:
I have one DataGrid with one column colPWD, and the code DataGridTableStyle dgStyle = new DataGridTableStyle(); DataGridTextBoxColumn colPWD = new DataGridTextBoxColumn(); colPWD.MappingName =...
3
by: Agnes | last post by:
How Can I set the max length in the datagrid ? Thanks
5
by: Datagridtextboxcolumn not firing event | last post by:
"PLEASE HELP, I NEED HELP N O W !!!" AddHandler dgtxtboxcolumn.textbox.KeyDown, AddressOf TextBoxKeyPress Private Sub TextBoxKeyPress(ByVal sender as Object, ByVal e as KeyEventArgs) End...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.