data grid view
-
You are using the list
t
as the source of your index count, rather thandataGridView1
, it should be:for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[0]["Address"].ToString();
}(or similar).
Veni, vidi, abiit domum
is this right ? String sql = "select Name,Gender,Course1,Course2 from Info "; DataTable t = DataAccess.GetDataTable(sql); MessageBox.Show(Convert.ToString(dataGridView1.Rows.Count)); for (int i = 0; i <t.Rows.Count; i++) { dataGridView1.Rows.Add(); dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[i]["Name"]; dataGridView1.Rows[i].Cells["Column2"].Value = t.Rows[i]["Gender"]; dataGridView1.Rows[i].Cells["Column3"].Value = t.Rows[i]["Course1"]; dataGridView1.Rows[i].Cells["Column4"].Value = t.Rows[i]["Course2"]; }
-
is this right ? String sql = "select Name,Gender,Course1,Course2 from Info "; DataTable t = DataAccess.GetDataTable(sql); MessageBox.Show(Convert.ToString(dataGridView1.Rows.Count)); for (int i = 0; i <t.Rows.Count; i++) { dataGridView1.Rows.Add(); dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[i]["Name"]; dataGridView1.Rows[i].Cells["Column2"].Value = t.Rows[i]["Gender"]; dataGridView1.Rows[i].Cells["Column3"].Value = t.Rows[i]["Course1"]; dataGridView1.Rows[i].Cells["Column4"].Value = t.Rows[i]["Course2"]; }
-
is this right ? String sql = "select Name,Gender,Course1,Course2 from Info "; DataTable t = DataAccess.GetDataTable(sql); MessageBox.Show(Convert.ToString(dataGridView1.Rows.Count)); for (int i = 0; i <t.Rows.Count; i++) { dataGridView1.Rows.Add(); dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[i]["Name"]; dataGridView1.Rows[i].Cells["Column2"].Value = t.Rows[i]["Gender"]; dataGridView1.Rows[i].Cells["Column3"].Value = t.Rows[i]["Course1"]; dataGridView1.Rows[i].Cells["Column4"].Value = t.Rows[i]["Course2"]; }
-
Kawshik_itbd wrote:
is this right ?
It looks like it probably is; what happens when you try it? I would also ask why you are not using databinding to build your view automatically.
Veni, vidi, abiit domum
it works .how i use databinding? can you gives some example of code ??
-
Should be ok, provided you want to append every time you call this function. Else you will probably have to clear the GridControl first.
The good thing about pessimism is, that you are always either right or pleasently surprised.
can you gives some basics about GridControl?
-
it works .how i use databinding? can you gives some example of code ??
-
There are lots of samples and articles around that explain databinding and how it can speed up your applications.
Veni, vidi, abiit domum
the way that i follow is n;t correct or not ??
-
the way that i follow is n;t correct or not ??
-
Yes, it's correct, in the sense that it does what you need, but databinding is much more powerful and you can do it with a single line of code.
Veni, vidi, abiit domum
thnaks for advice :)
-
Kawshik_itbd wrote:
for (int i = 0; i < t.Rows.Count; i++) { dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[0]["Address"].ToString(); }
Change to
for (int i = 0; i
This should do it..
The signature is in building process.. Please wait...
you can also write like this.. //this line write before for loop //with this line grid(dataGridView1) create line as per dataTable(t) dataGridView1.RowCount=t.Rows.Count for (int i = 0; i < t.Rows.Count; i++) { dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[0]["Address"].ToString(); }
-
for (int i = 0; i < t.Rows.Count; i++)
{dataGridView1.Rows\[i\].Cells\["Column1"\].Value = t.Rows\[0\]\["Address"\].ToString(); }
error occur :
index was out of range,must be non-negative and less than the
size of the collection. parameter name:indexhow i solve this prob,
you can also write like this.. //this line write before for loop //with this line grid(dataGridView1) create line as per dataTable(t) dataGridView1.RowCount=t.Rows.Count for (int i = 0; i < t.Rows.Count; i++) { dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[0]["Address"].ToString(); }
-
you can also write like this.. //this line write before for loop //with this line grid(dataGridView1) create line as per dataTable(t) dataGridView1.RowCount=t.Rows.Count for (int i = 0; i < t.Rows.Count; i++) { dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[0]["Address"].ToString(); }
it does not show the multiple it shows only first row ,if i did like for (int i = 0; i <t.Rows.Count; i++) { dataGridView1.Rows.Add(); dataGridView1.Rows[i].Cells["Column1"].Value = t.Rows[i]["Name"]; dataGridView1.Rows[i].Cells["Column2"].Value = t.Rows[i]["Gender"]; dataGridView1.Rows[i].Cells["Column3"].Value = t.Rows[i]["Course1"]; dataGridView1.Rows[i].Cells["Column4"].Value = t.Rows[i]["Course2"]; } no prob occur.