3 Replies - 5278 Views - Last Post: 14 July 2009 - 07:32 AM Rate Topic: -----

#1 izzahaf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 12-March 09

paste from clipboard to gridview

Posted 13 July 2009 - 07:09 PM

need help again.

i try to paste from clipboard to datagridview, and it works.
but in odd row at last cell,the value added by a flag. i'm confused to explain, but i think u understand.

for example ;

1234-----------------> in cell : 1234... (... is flag that i can't describe here)

try
			{
				string s = Clipboard.GetText();
				string[] lines = s.Split('\n');
				int iFail = 0, iRow = grdVariable.CurrentCell.RowIndex;
				int iCol = grdVariable.CurrentCell.ColumnIndex;
				DataGridViewCell oCell;
				int flag=1;
				foreach (string line in lines)
				{
					
					if (iRow < grdVariable.RowCount && line.Length > 0)
					{
						string[] sCells = line.Split('\t');
						for (int i = 0; i < sCells.GetLength(0); ++i)
						{
							if (iCol + i < this.grdVariable.ColumnCount)
							{
								oCell = grdVariable[iCol + i, iRow];
								if (!oCell.ReadOnly)
								{
									if (oCell.Value == null) oCell.Value = "";
									//if (flag % 2 == 0 && i == sCells.GetLength(0) - 1) sCells[i] = sCells[i].Substring(0, sCells.Length - 1);
									if (oCell.Value.ToString() != sCells[i])
									{
										oCell.Value = Convert.ChangeType(sCells[i], oCell.ValueType);										
									}
									else
										iFail++;//only traps a fail if the data has changed and you are pasting into a read only cell
								}
							}
							else
							{ break; }
						}
						iRow++;
					}
					else
					{ break; }
					if (iFail > 0)
						MessageBox.Show(string.Format("{0} updates failed due to read only column setting", iFail));
					flag++;
				}
			}
			catch (FormatException)
			{
				MessageBox.Show("The data you pasted is in the wrong format for the cell");
				return;
			}


(this code is taken from codeproject)


thanks

Is This A Good Question/Topic? 0
  • +

Replies To: paste from clipboard to gridview

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: paste from clipboard to gridview

Posted 13 July 2009 - 08:16 PM

Either set the width of the DataGridViewColumn to be wider than the text it will contain by setting the Width property or set the Resizable property to true, then you can manually resize the column during runtime.
Was This Post Helpful? 0
  • +
  • -

#3 izzahaf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 12-March 09

Re: paste from clipboard to gridview

Posted 14 July 2009 - 02:02 AM

View PostJayman, on 13 Jul, 2009 - 07:16 PM, said:

Either set the width of the DataGridViewColumn to be wider than the text it will contain by setting the Width property or set the Resizable property to true, then you can manually resize the column during runtime.



not because of it. there's a flag only in 'odd row at last cell'.
but the value is right.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: paste from clipboard to gridview

Posted 14 July 2009 - 07:32 AM

Then I don't know what it could be, you will need to debug it and see what data the clipboard is providing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1