469,358 Members | 1,567 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

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 4931
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

Post your reply

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

Similar topics

2 posts views Thread by Job Lot | last post: by
reply views Thread by Mike Burkhart | last post: by
1 post views Thread by Sangeetha | last post: by
3 posts views Thread by Agnes | last post: by
5 posts views Thread by Datagridtextboxcolumn not firing event | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.