I have 2 forms: The first form has textboxes that display different task names and times spent on each task. The second form has a datagridView. Using:
public void DataTableTest()
{
//Create DataTable
DataTable dt = new DataTable();
//Add Columns
dt.Columns.Add("Task Name", typeof(string));
dt.Columns.Add("Time Worke (HH:mm:ss)", typeof(string));
//Add Rows
dt.Rows.Add();
dataGridView1.DataSource = dt;
}
I am able to get the data table layout the way I want. My issue is, I want to pull the textbox.text from Form1 to populate the [0][0] cell of the data table. Something like:
//Add Rows
dt.Rows.Add();
dt.Rows[0][0] = Form1.tasktextbox1.text;
dataGridView1.DataSource = dt;
But this is not working. I am getting the following from my error list when I try to start the program:
'TaskTracker.Form1.TaskTextBox1' is inaccessible due to its protection level.
Thank you in advance for the assistance. If you have any questions ask, first time posting and new to programming.
The error describes itself: TaskTextBox1 is a private member of Form1 and you cannot call it on another class. Add a property to Form1:
Public string task1text { get { return this.textbox1.text; }}
and then use it on the other form:
dt.Rows[0][0] = Form1.task1text;
I was able to complete this task with:
public static string task1text;
public void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
task1text = TaskTextBox1.Text;
ReportForm RF = new ReportForm();
RF.Show();
}
on form1 and using Roozbeh's suggestion on the second form:
dt.Rows[0][0] = Form1.task1text;
Thank you all for your assistance. Slowly figuring it out! :)
Related
Good day. I have passed a data variable from one class to another to put into a datagridview in the main form. I put some message boxes in each case to know that it accesses the said function and that the data is clearly passed. But when I run the program. The table doesn't put the data inside it.
Here is the code when I pass the data
if (txtCode1.ElementAt(intCtr + 1).Equals(val4)) {
MessageBox.Show("Lol");
Compilourdes_GUI cmp = new Compilourdes_GUI();
cmp.AddtotblLexeme(val2, val2);
break;
}
And here is the code of AddtotblLexeme
public void AddtotblLexeme(string lexeme, string token) {
MessageBox.Show(lexeme+" "+token);
tblLexeme.Rows.Add(lexeme , token); //adding tokens and lexeme to the table
}
Code where I made the DataTable
private void Start()
{
tbl1.AutoGenerateColumns = true;
tbl1.DataSource = null;
tbl1.Rows.Clear();
InitTable();
string txtCode1 = txtCode.Text;
LexicalAnalyzer lex = new LexicalAnalyzer(txtCode1);
lex.StartLex();
tbl1.DataSource = tblLexeme;
}
public void InitTable()
{
tblLexeme = new DataTable();
tblLexeme.Columns.Add("Lexeme", typeof(string));
tblLexeme.Columns.Add("Token", typeof(string));
}
DataTable tblLexeme = new DataTable();
Here is the image of the output . the "TEST" word/s should be inside the table, but as you can see, it didn't get put in.
Ok I think I understand your problem. If you added the columns directly in the designer, my guess is that you added unbound columns. If so, then the DataGridView cannot match up the row you are adding to the rows in the table. To fix this, delete the columns from the DatagridView. Then make sure that your DataGridView has property AutoGenerateColumns = true, before setting DataSource = tblLexeme. Now two things happen automatically: firstly the DataGridView picks up the columns from your DataTable; and secondly, when adding a new row to the DataTable, it should show automatically in the DataGridView.
In AddtotblLexeme, for testing purposes, can you please add, in place of your Rows.Add():
DataRow nR = tblLexeme.NewRow();
nR[0] = lexeme;
nR[1] = token;
tblLexeme.Rows.Add(nR);
Then in debugger check that nR does have an ItemArray with 2 columns.
I am creating a WPF Application that has 2 windows.
In Window_1 i have a button that imports an excel document and stores corresponding fields in a DataTable. After this excel document has been successfully imported i want to prompt user to view the document. If the user says 'Yes' then Window_2 is loaded.
In Window_2 there is a DataGrid and i want it to be populated using the DataTable in Window_1?
How do i populate the DataGrid in Window_2 using data stored in the DataTable in Window_1
i think i have found a solution to my problem. (I have derived this solution from ASh's response.)
In Window_1
DataTable maindata = new DataTable(); //datatable with imported excel data
//button click event to prompt user to view Window_2
private void yes_Click(object sender, RoutedEventArgs e){
var w2 = new Window { dt = maindata };
w2.Show();
}
In Window_2
public DataTable dt { get; set; }
private void Window_Loaded(object sender, RoutedEventArgs e)
{
dtGrid.ItemsSource = dt.DefaultView; //dtGrid is the DataGrid
}
So here is what i think.
Upon click 'Yes' (yes_Click) the data in maindata DataTable is equated to the data in dt DataTable (present in Window_2). The latter DataTable gets the data of maindata DataTable and loads it into DataGrid dt.
I thought i should put out my findings in case another person runs into the same problem.
Thank you all.
You could give the data from window 1 to window 2 like this:
var data = //your data;
Window_2 window = new Window_2(data);
window.Show();
//then in window 2
public Window_2(var data){
InitializeComponent();
this._fieldWithData = data;
}
So make a constructor overload where you pass the data. Then you can pass through the data from the different windows. StackOverflow is here to help to figure out on how to actually fill the datagrids, plenty of posts about that! Hope that helps!
This is the version of the code that works for me in a similar scenario.
In the first Window, in .xaml.cs I would place this:
DataTable myDataTable=new DataTable();
//fill in myDataTable with anything you want. Your case is Excel data
private void btnExtractComplementaryBOM_Click(object sender, RoutedEventArgs e)
{
ShowDataGrid window2 = new ShowDataGrid(myTable) ; //open up an instance of an existing Window called ShowDataGrid. Pass to it as an argument the dataTable object which embeds data from your Excel
ShowDataGrid.Show();
}
In the second Window (which can be customized as you wish by its .xaml).
You should have the following lines of code in ShowDataGrid.xaml.cs
public partial class ShowDataGrid : Window //2nd Window is called "ShowDataGrid" here
{
public DataTable dataTable2; //declare a public datatable variable
public ShowDataGrid(DataTable dataTable2)
{
InitializeComponent();
datagridName.ItemsSource = dataTable2.DefaultView;
}
}
From one window you can get the other window by name, then get the datagrid by name (as long as you have named everything).
Window w = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.Name == "windowname");
DataGrid dg = (DataGrid)w.FindName("datagridname");
Then 'dg' is a reference to the datagrid that you want.
I can't fix this error that I get A Relation named 'PlaneAirline' already belongs to this DataSet. I have tried to change the name of the relation but I get the same error
Here is my code:
private void getData()
{
SqlDataAdapter parentDataAdapter = new SqlDataAdapter("select * from Airline", connection);
parentDataAdapter.Fill(ds, "Airline");
SqlDataAdapter childDataAdapter = new SqlDataAdapter("select * from Plane", connection);
childDataAdapter.Fill(ds, "Plane");
DataColumn parentColumn = ds.Tables["Airline"].Columns["airline_id"];
DataColumn childColumn = ds.Tables["Plane"].Columns["airline_id"];
rel = new DataRelation("PlaneAirline", parentColumn, childColumn);
ds.Relations.Add(rel);
parentBindingSource.DataSource = ds;
parentBindingSource.DataMember = "Airline";
childBindingSource.DataSource = parentBindingSource;
childBindingSource.DataMember = "PlaneAirline";
}
private void dg_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void AirlineReservation_Load(object sender, EventArgs e)
{
parentDataGridView.DataSource = parentBindingSource;
childDataGridView.DataSource = childBindingSource;
getData();
}
Could you please help me
I think your problem is that you are calling GetData() method on CellContentClick event. This event will get fired when the content within a cell is clicked. Hence whenever you are clicking the cell your code is trying to add a relation which is already present when you first clicked a cell.
Probably you need to move the GetData() call to Form Load as given in the tutorial you have given.
Hope this helps.
On every click in your DataGrid (I'm assuming that dg_CellContentClick is tha click handler for the data grid cell) you're calling getData, where you try to add the relation PlaneAirlineover and over again.
Try to move the lines
rel = new DataRelation("PlaneAirline", parentColumn, childColumn);
ds.Relations.Add(rel);
to where you initially create or define your variable ds (I think it's a DataSet).
I have a DataGridView tied to a DataTable source. Among the data on the elements in the table is a Guid which I want to remain hidden. (It's used internally for reference, but should not be displayed.) The code I'm using to create the table is as follows:
private DataTable mEntities = new DataTable();
private System.Windows.Forms.DataGridView EntitiesGridView;
These are declared elsewhere, just showing here for reference.
private void BuildEntityTable()
{
mEntityTable.Columns.Add("id", typeof(Guid));
mEntityTable.Columns.Add("Name", typeof(string));
... (some other columns)
foreach (Foo foo in mEntities)
{
DataRow row = mEntityTable.NewRow();
row["id"] = foo.id;
row["Name"] = foo.Name;
... (rest of data)
mEntityTable.Rows.Add(row);
}
DataColumn[] entityKeys = new DataColumn[1];
entityKeys[0] = entityTable.Columns["id"];
mEntityTable.PrimaryKey = entityKeys;
EntitiesGridView.DataSource = mEntityTable.DefaultView;
EntitiesGridView.Columns["id"].visible = false;
}
So far so good. The table is created, and there's no visible "id" column. However, if I later add a new object to the table, we run into trouble. The code is almost the same:
void AddNewObject(object sender, MyArgs e)
{
Foo foo = e.Foo;
lock (mEntities)
{
mEntities.Add(foo);
}
lock (mEntityTable)
{
DataRow row = mEntityTable.NewRow();
row["id"] = foo.id;
row["Name"] = foo.Name;
... (rest of data)
mEntityTable.Rows.Add(row);
}
}
For some reason, this makes the "id" column come back. I've tried copying the EntitiesGridView.Columns["id"].visible = false; line from the previous code, but it does no good. Nothing I do after this point will make that column go away and stay gone. Any clues what I'm missing?
just write this line
datagridview1.Columns[0].visible = false;
call this event in your form_load()
private void dgv_DataBindingComplete(Object sender, DataGridViewBindingCompleteEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
dgv.Columns[3].Visible = false;
}
I have also encountered this problem, but found that you can make your datagridview changes at design time and then save the project. Run the application, then quit from the application. The dgv on the design form has now automatically changed its display. Close the form and reopen it and you will see that the columns you originally included/excluded are returned. No additional code is required for this fix.
I have a problem with the UltraGrid control from Infragistics. I have created a ultracombobox with a few values in it:
UltraCombo ultraComboPaneel = new UltraCombo();
ultraComboPaneel.DataSource = articleList;
ultraComboPaneel.ValueMember = "ArticleID";
ultraComboPaneel.DisplayMember = "Name";
Now I have an UltraGrid, and I want to put the ultraCombo in a cell so I can choose one of the items of the ultracombo as a cell value. I tried it both in code and in the ultragrid designer but i can't seem to find a way to do it.
Any of you got an idea? More information can be provided if needed
Edit:
I found something like
UltraGridColumn ugc = ultraGridTypePaneel.DisplayLayout.Bands[0].Columns.Add("combo");
ultraGridTypePaneel.DisplayLayout.Bands[0].Columns["combo"].EditorControl = ultraComboPaneel;
I feel I'm on the right way but it is still not showing on the screen...
The UltraCombo provides a great deal of functionality. If all you need is the ability to choose an item from a list, you might find the grid's ValueLists provide a better solution.
Here's some code to get you started:
private void myGrid_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
const string colorValueList = #"ColorValueList";
if (!e.Layout.ValueLists.Exists(colorValueList))
{
ValueList svl = e.Layout.ValueLists.Add(colorValueList);
svl.ValueListItems.Add(1, "Red");
svl.ValueListItems.Add(2, "Green");
svl.ValueListItems.Add(3, "Blue");
}
e.Layout.Bands[0].Columns["Color"].ValueList = e.Layout.ValueLists[colorValueList];
}
You could find at the link below some approaches that you could use to put a DropDown into a UltraGrid cell:
http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.aspx?ArticleID=7841
Going back to your current code snippet - you are almost there:
First you should set the binding context of your UltraCombo to the BindingContext of the form the your UltraCombo will be used like:
ultraComboPaneel.BindingContext = this.BindingContext;
Please note that setting binging context should happen prior setting your control to be EditorControl. One more thing that I noticed is that the property currently is changed to EditorComponent so I believe that you are using older version of the Infragistics components. However you should still be able to use the very same approach. I have created small code snippet showing the above with code:
public partial class Form1 : Form
{
UltraCombo uc;
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("Int", typeof(int));
dt.Rows.Add(1);
dt.Rows.Add(1);
dt.Rows.Add(1);
DataTable dtt = new DataTable();
dtt.Columns.Add("Int", typeof(int));
dtt.Rows.Add(2);
dtt.Rows.Add(2);
dtt.Rows.Add(2);
uc = new UltraCombo();
uc.BindingContext = this.BindingContext;
uc.DataSource = dtt;
ultraGrid1.DataSource = dt.DefaultView;
}
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
e.Layout.Bands[0].Columns[0].EditorComponent = uc;
}
}
Hope this helps.
I use the Ultra Dropdown instead.
dim udd As UltraDropDown
udd = New UltraDropDown
With udd
'add data binding or value list items here
End With
Me.ultragrid.DisplayLayout.Bands(0).Columns("Column Name").ValueList = udd
The key is the last line that assigns the "Value List" of the ultra grid column to the Drop down control.